简体   繁体   English

应用程序委托-卸载视图控制器

[英]App Delegate - Unload View Controller

I'm trying to unload a view controller from view when the iPhone goes to sleep. 我试图在iPhone进入睡眠状态时从视图中卸载视图控制器。 My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out. 我的应用程序有一个秒表,当手机进入睡眠状态或有来电时,或者用户关闭应用程序而未注销时,它必须一直计数。

I have all this functionality in place, I'm capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. 我已具备所有这些功能,正在捕获所有开始时间和停止时间,并在重新进入秒表视图控制器时计算差值。 It all works beautifully. 一切都很漂亮。 When I was doing some additional testing I realised I hadn't catered for the iPhone going into sleep mode. 当我进行一些其他测试时,我意识到我并没有考虑iPhone进入睡眠模式。

So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. 因此,我要做的就是确保秒表正确无误,将用户带回应用程序主屏幕。 I know the following method is called when the app goes to sleep: 我知道应用程序进入睡眠状态时会调用以下方法:

-(void)applicationWillResignActive:(UIApplication *)application

How do I unload the stopwatch view controller from my app delegate ? 如何从应用程序委托中卸载秒表视图控制器?

---- UPDATE ---- ----更新----

kpower, thanks for your feedback. kpower,感谢您的反馈。 I've implemented the following code: 我已经实现了以下代码:

In my App Delegate: 在我的应用程序委托中:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppIsAsleep" object:nil];
}

In my view controller, I have the following: 在我的视图控制器中,我有以下内容:

-(void)viewDidLoad 
{   
    // Add Observer.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidUnload:) name:@"AppIsAsleep" object:nil];
}

- (void)viewDidUnload {
    //Remove the Observer.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AppIsAsleep" object:nil];
}

When the phone goes to sleep, it actually closes the app, am I doing something wrong ? 手机进入睡眠状态后,实际上会关闭应用程序,我做错了吗?

Regards, Stephen 问候斯蒂芬

You can use the Notifications mechanism. 您可以使用通知机制。 It allows you to unload view controller from different place (not the AppDelegate) this case. 在这种情况下,它允许您从其他位置(而不是AppDelegate)卸载视图控制器。

For example, in your view controller's viewDidLoad method you add an observer (don't forget to remove it in viewDidUnload ) and in applicationWillResignActive: method of AppDelegate you just simply post notification. 例如,在视图控制器的viewDidLoad方法中添加观察者(不要忘记在viewDidUnload中将其删除),而在AppDelegate的applicationWillResignActive:方法中,您只需发布通知即可。 That's all. 就这样。

↓ Update here ↓ ↓在这里更新↓

When you get a notification - you should manage view controller's removing by yourself. 收到通知时-您应该自己管理视图控制器的删除。 And calling viewDidUnload here is not the solution, cause this method is called after view controller was already unloaded and doesn't cause removing. 并且在这里调用viewDidUnload并不是解决方案,因为此方法已在视图控制器已卸载后调用,并且不会导致删除。

How to remove? 如何删除? Depends on how the view controller was added (for example, popViewControllerAnimated for UINavigationController). 取决于添加视图控制器的方式(例如,UINavigationController的popViewControllerAnimated )。 The main idea here is to make object's retain count equal to 0 (as you know this case an object will be destroyed) - so you should sent release message necessary amount of times. 这里的主要思想是使对象的保留计数等于0(如您所知,对象将被销毁)-因此,您应该向释放消息发送必要的次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM