Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序委托中使用实例方法_Ios_Methods_Instance_Appdelegate - Fatal编程技术网

Ios 在应用程序委托中使用实例方法

Ios 在应用程序委托中使用实例方法,ios,methods,instance,appdelegate,Ios,Methods,Instance,Appdelegate,当我的应用程序进入后台模式并退出时,我想从我的应用程序委托中的BPGameController类中运行一个名为“pauseGame”的实例方法,当应用程序再次激活时,运行一个名为“resumeGame”的实例方法,我尝试了一些不同的方法,但到目前为止,在BPGameController的viewDidLoad方法中没有一种方法对我有效。请按如下方式注册通知: [[NSNotificationCenter defaultCenter] addObserver:self selector:@sele

当我的应用程序进入后台模式并退出时,我想从我的应用程序委托中的BPGameController类中运行一个名为“pauseGame”的实例方法,当应用程序再次激活时,运行一个名为“resumeGame”的实例方法,我尝试了一些不同的方法,但到目前为止,在BPGameController的viewDidLoad方法中没有一种方法对我有效。请按如下方式注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseGame)
                                             name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resumeGame)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];
当你完成时,不要去观察

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

获取对现有
BPGameController
实例的引用并调用该方法。为什么要从应用程序委托执行此操作?将控制器设置为响应相关通知。您忽略了此答案中真正重要的部分。你必须在
dealoc
方法中删除观测者,否则应用程序可能会在某个时候崩溃。我添加了它以获得更完整的答案。