2016-12-30 118 views

回答

0

檢查這個代碼(如果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]; 
} 
+0

我需要Objective-C – kiran

1

我猜你只需要

[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(refreshItems)name:UIApplicationDidBecomeActiveNotification object:nil];

0
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]; 
相關問題