ios - 如何从我的应用程序委托(delegate)启动一个 NSTimer,它调用我在 Viewcontroller.m 中的方法

标签 ios objective-c uiviewcontroller nstimer appdelegate

当我的应用程序关闭时,我希望它在 viewController.m 中调用 void 方法

我试过这个:

我已经声明了 NSTimer (警报)和void(alarm:)viewController.h我已经导入了viewController.hAppdelegate.m

AppDelegate.m:

- (void)applicationDidEnterBackground:(UIApplication *)application{

    alarmm = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(alarm:) userInfo:nil repeats:YES];

}

但是当它运行时
**2014-12-20 13:53:19.881 protect my phone[3292:959295] -[AppDelegate alarm:]: unrecognized selector sent to instance 0x170043810
2014-12-20 13:53:19.884 protect my phone[3292:959295] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate alarm:]: unrecognized selector sent to instance 0x170043810'
*** First throw call stack:
(0x182e5659c 0x1935600e4 0x182e5d664 0x182e5a418 0x182d5eb6c 0x183d2ae18 0x182e0e8d8 0x182e0e588 0x182e0bfd4 0x182d390a4 0x18bee35a4 0x18766e3c0 0x10000a320 0x193bcea08)
libc++abi.dylib: terminating with uncaught exception of type NSException**

最佳答案

正如你所说,你声明了 alarm在某些 View Controller 中,但您尝试在 AppDelegate 上调用它, 没有定义的地方。
因此,您会因 self 上无法识别的选择器而崩溃。

尝试在 NSTimer 中交换自我创建到 View Controller 引用...

关于ios - 如何从我的应用程序委托(delegate)启动一个 NSTimer,它调用我在 Viewcontroller.m 中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27580627/

相关文章:

iphone - objective-c : for-loop gets slower and slower

ios - 如何将 iPhone 应用程序常量(.h 类)和第三方框架添加到 watch 套件扩展中?

ios - Swift:尝试打印 CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"时出现异常

ios - 修改数据模型后xcode更新类

ios - 通知 UITableView dataSource 的变化

ios - 如何显示AVPlayer播放音频的进度

objective-c - 如何使用pdf文件缩放webView

objective-c - 使用选项卡栏 Controller ( Storyboard)阻止访问 UIViewControllers

SwiftUI - 使用 'pushViewController' 时出现 fatal error

iPhone sdk 在 View Controller 之间传递消息