如何在應用程序進入後臺並更改某些活動(如移動時間或日期)和前臺(應用程序活動模式)時調用方法。當應用程序進入後臺並進入前臺時調用UIViewController方法
然後我想調用的UIViewController類
FirstViewController類方法的方法。
-(void)refreshItems{
// Your item refresh code.
}
如何在應用程序進入後臺並更改某些活動(如移動時間或日期)和前臺(應用程序活動模式)時調用方法。當應用程序進入後臺並進入前臺時調用UIViewController方法
然後我想調用的UIViewController類
FirstViewController類方法的方法。
-(void)refreshItems{
// Your item refresh code.
}
檢查這個代碼(如果FirstViewController是窗口的RootViewController的其他人採取FirstViewController的實例中的AppDelegate和檢查空)
func applicationDidEnterBackground(_ application: UIApplication) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let controller = appDelegate.window?.rootViewController as? FirstViewController {
controller.refreshItems()
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let controller = appDelegate.window?.rootViewController as? FirstViewController {
controller.refreshItems()
}
}
在
Objective-C的
- (void)applicationDidEnterBackground:(UIApplication *)application {
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
FirstViewController *controller = (FirstViewController*)[[appDelegate window] rootViewController];
[controller refreshItems];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
FirstViewController *controller = (FirstViewController*)[[appDelegate window] rootViewController];
[controller refreshItems];
}
我猜你只需要
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(refreshItems)name:UIApplicationDidBecomeActiveNotification object:nil];
you have set notification in appDelegate class
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:@"forground" object:nil];
}
and add observer to your viewcontroller class viewdidload() methos
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showMainMenu:)
name:@"refreshItems" object:nil];
我需要Objective-C – kiran