警告:“UIViewController”可能无法响应“xxx”

发布于 2024-11-05 13:46:44 字数 1233 浏览 3 评论 0原文

我对 iPhone 开发还比较陌生,但我知道这个警告通常是由于我的类头文件中没有声明方法而导致的。这略有不同——至少我认为是这样。

我在我的应用程序根视图控制器中创建了一个自定义选项卡栏,它在委托方法内动态加载其他视图控制器 - 本质上是这样的:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    UIViewController *viewController = [viewControllers objectAtIndex:item.tag];
    [self.selectedViewController.view removeFromSuperview];
    [self.view insertSubview:viewController.view atIndex:0];
    self.selectedViewController = viewController;

}

该代码工作正常并加载到所需的视图中。当视图更改时,将运行检查以查看设置视图是否即将被卸载,如果是,则调用保存设置方法,如下所示:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    if (self.currentController == 1) {
        [self.selectedViewController saveSettings];
    }

    UIViewController *viewController = [viewControllers objectAtIndex:item.tag];
    [self.selectedViewController.view removeFromSuperview];
    [self.view insertSubview:viewController.view atIndex:0];
    self.selectedViewController = viewController;

}

代码再次运行良好,并且调用了 SettingsViewController 的实例方法,但是因为该方法位于 SettingsViewController 的标头中,而不是 RootViewController 中,因此会出现警告。

如果我也在 RootViewController 中声明它,我会收到“无匹配方法声明”警告。我认为重新声明我的函数可以解决该警告 - 但肯定不是解决此问题的“正确”方法。

I'm still relatively new to iPhone development but I know this warning is usually the result of not declaring a method in my classes header file. This is slightly different - at least I think it is.

I've created a custom tab bar in my applications root view controller which loads in the other view controllers dynamically inside the delegate method - essentially like this:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    UIViewController *viewController = [viewControllers objectAtIndex:item.tag];
    [self.selectedViewController.view removeFromSuperview];
    [self.view insertSubview:viewController.view atIndex:0];
    self.selectedViewController = viewController;

}

That code works fine and loads in the required views. When the view changes a check is run to see if the setting view is about to be unloaded and if so it calls the save settings method like this:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

    if (self.currentController == 1) {
        [self.selectedViewController saveSettings];
    }

    UIViewController *viewController = [viewControllers objectAtIndex:item.tag];
    [self.selectedViewController.view removeFromSuperview];
    [self.view insertSubview:viewController.view atIndex:0];
    self.selectedViewController = viewController;

}

Again the code functions fine and the instance method of SettingsViewController is called, but because the declaration of the method is in the header of the SettingsViewController and not the RootViewController hence the warning.

If I declare it in the RootViewController as well I get the 'no matching method declaration' warning. I presume redeclaring my function would fix the warning - but surely isn't the 'proper' way to fix this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混浊又暗下来 2024-11-12 13:46:44

如果我正确理解你的问题,这应该有效:

if (self.currentController == 1) {
    [(SettingsViewController *)self.selectedViewController saveSettings];
}

If I understand your question correctly, this should work:

if (self.currentController == 1) {
    [(SettingsViewController *)self.selectedViewController saveSettings];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文