【问题标题】:UIViewController method from AppDelegate来自 AppDelegate 的 UIViewController 方法
【发布时间】:2014-11-06 16:19:47
【问题描述】:

这是我在 stackoverflow.com 上的第一个问题。 我正在尝试从 AppDelegate.m 中的 ViewController.m 调用方法 [- (void)alterTime]。是的,我在 AppDelegate.h 中有 #imported "ViewController.h"。将控制器导入 AppDelegate.m 没有任何区别。此外,“后台提取”已开启。

这是我正在尝试实现的 AppDelegate.m 中的代码:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    NSLog(@"Current Time Altered");

    //Get current view controller
    ViewController *mainViewController = (ViewController *)self.window.rootViewController;
    [mainViewController alterTime];

    //Cleanup
    completionHandler(UIBackgroundFetchResultNewData);
}

这是给我带来问题的代码:

[mainViewController alterTime];

它声明:“'ViewController' 没有可见的@interface 声明选择器'alterTime'”。

【问题讨论】:

  • 显示ViewController.h

标签: objective-c methods uiviewcontroller appdelegate


【解决方案1】:

所以这个错误意味着它在ViewController类中找不到方法名(选择器)。

确保在 ViewController 标头中声明了 alterTime 方法,有效地将其公开。

如果没问题,请仔细检查以确保您的 rootViewControllerViewController 类型(拥有者)

【讨论】:

  • 非常感谢。这次我的注意力不集中真的让我失望了。
  • 完全没有问题 :) 发生
【解决方案2】:

在您的 ViewController.h 文件中声明 - (void)alterTime

@interface ViewController : UIViewController

- (void)alterTime;

@end

【讨论】:

  • 天哪,我的注意力不集中将是我的末日。我为没有看到自己而感到尴尬。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多