2016-12-30 12 views
0

как вызвать метод, когда приложение переходит в фоновый режим и изменяет некоторую активность, например, время или дату мобильного телефона и выходит на передний план (активный режим приложения).Вызвать метод UIViewController, когда приложение заходит в фоновый режим и выходит на передний план

Тогда я хочу, чтобы вызвать метод в классах UIViewController

FirstViewController метод класса.

-(void)refreshItems{ 
// Your item refresh code. 
} 

ответ

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) имя: 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];