ios 如何在 UIViewController 中使用 applicationDidBecomeActive?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11928126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use applicationDidBecomeActive in UIViewController?
提问by js_
I want to reload data in UIViewController when application become active or become foreground.
当应用程序变为活动状态或变为前台时,我想在 UIViewController 中重新加载数据。
I know applicationDidBecomeActive is called in AppDelegate class.
But I have to have a global variable for the UIViewController to reload its data in AppDelegate class like this code:
我知道 applicationDidBecomeActive 在 AppDelegate 类中被调用。
但是我必须有一个全局变量让 UIViewController 重新加载它在 AppDelegate 类中的数据,如下代码:
in AppDelegate.m
// global variable
UIViewController *viewController1;
UIViewController *viewController2;
-(void)applicationDidBecomeActive:(UIApplication *)application
{
[viewController1 reloadData];
[viewController2 reloadData];
}
But it is inconvenient especially when I have a lot of UIViewControllers.
但这很不方便,尤其是当我有很多 UIViewController 时。
Can I use applicationDidBecomeActive in UIViewController instead of in AppDelegate class?
Or are there better ways than having global variable for UIViewController?
我可以在 UIViewController 而不是 AppDelegate 类中使用 applicationDidBecomeActive 吗?
或者有比为 UIViewController 设置全局变量更好的方法吗?
I also need to use the following method from UIViewControllers:
我还需要使用 UIViewControllers 中的以下方法:
-(void)applicationWillResignActive:(UIApplication *)application
-(void)applicationDidEnterBackground:(UIApplication *)application
-(void)applicationWillEnterForeground:(UIApplication *)application
回答by Apurv
At the time of reactivation, if you want to carry a particular thing for a view controller, you should register a notification in its viewDidLoad
method.
在重新激活的时候,如果你想为一个视图控制器携带一个特定的东西,你应该在它的viewDidLoad
方法中注册一个通知。
UIApplicationDidBecomeActiveNotification
will automatically notify your application and given controller, if they registered for it.
UIApplicationDidBecomeActiveNotification
如果他们注册了,将自动通知您的应用程序和给定的控制器。
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];
回答by Adrian Macneil
Here is an example of registering a notification handler in Swift (adapted from Apurv's answer above):
这是在 Swift 中注册通知处理程序的示例(改编自上面 Apurv 的回答):
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidBecomeActive(notification:)),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
// do something
}
Update for Swift 4.2
:
更新Swift 4.2
:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc func applicationDidBecomeActive(notification: NSNotification) {
// Application is back in the foreground
print("active")
}
回答by Danut Pralea
Swift 3:
斯威夫特 3:
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidBecomeActive(_:)),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
func applicationDidBecomeActive(_ notification: NSNotification) {
// do something
}
Note: Don't forget to remove the observer
注意:不要忘记移除观察者
回答by johnbakers
You cannot use applicationDidBecomeActive
in viewController; it is not a method for that class.
您不能applicationDidBecomeActive
在 viewController 中使用;它不是该类的方法。
However, you can use applicationDidBecomeActive
method in AppDelegate to call any methods in your view controller that you feel are important upon launch. Just keep a pointer to your controller so the App Delegate can reach it.
但是,您可以使用applicationDidBecomeActive
AppDelegate 中的方法来调用您认为在启动时很重要的视图控制器中的任何方法。只需保留一个指向您的控制器的指针,以便 App Delegate 可以访问它。
What those methods might be in your view controller is entirely up to you and the details of your program. Maybe it means calling a custom update method in your view controller or whatever else you feel is necessary.
这些方法可能在您的视图控制器中使用什么完全取决于您和您的程序的详细信息。也许这意味着在您的视图控制器中调用自定义更新方法或任何您认为必要的方法。
You could also use NSNotificationCenter as outlined here, with many system notifications available for application launch: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
您还可以使用此处概述的 NSNotificationCenter,其中有许多系统通知可用于应用程序启动:http: //developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
However, relying heavily on NSNotificationCenter is in my opinion a good way for an app to be come disorganized. If you call everything from your main methods in the AppDelegate only, you can always refer to that method to know exactly what your app is doing upon launch. If instead you use NSNotificationCenter, you could have actions spread across many classes/objects and it can be harder to track down what is going on. Since you mentioned multiple controller objects, I think it is more streamlined and organized to call everything from applicationDidBecomeActive
rather than register each viewcontroller for the same notification.
但是,在我看来,严重依赖 NSNotificationCenter 是使应用程序变得杂乱无章的好方法。如果您仅从 AppDelegate 中的主要方法调用所有内容,您始终可以参考该方法以准确了解您的应用程序在启动时正在做什么。相反,如果您使用 NSNotificationCenter,您的操作可能会分布在许多类/对象中,并且可能更难追踪正在发生的事情。由于您提到了多个控制器对象,我认为从调用所有内容applicationDidBecomeActive
而不是为相同的通知注册每个视图控制器更加精简和有组织。
回答by js_
Thank you all for answering my question.
But I found easier way to use applicationDidBecomeActive in UIViewController.
谢谢大家回答我的问题。
但我发现在 UIViewController 中使用 applicationDidBecomeActive 更简单。
@implementation AppDelegate
-(void)applicationDidBecomeActive:(UIApplication *)application
{
UIViewController<MyAppDelegate> *topViewController = (UIViewController<MyAppDelegate> *)navigationController.topViewController;
if ([topViewController respondsToSelector:@selector(MyApplicationDidBecomeActive)]) {
[topViewController MyApplicationDidBecomeActive];
}
}
@end
@protocol MyAppDelegate
@optional
-(void)MyApplicationDidBecomeActive;
@end