当应用程序进入后台并进入前台时调用UIViewController方法

时间:2016-12-30 09:27:01

标签: ios objective-c appdelegate background-foreground

如何在应用程序进入后台时调用方法并更改某些活动,如移动设备的时间或日期,并进入前台(应用程序活动模式)。

然后我想在UIViewController类中调用一个方法

FirstViewController类方法。

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

4 个答案:

答案 0 :(得分:1)

我想你只需要

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

答案 1 :(得分:0)

检查此代码(如果FirstViewController是窗口的rootViewController,则在AppDelegate中获取FirstViewController的实例并检查null)

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];
}

答案 2 :(得分: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];

答案 3 :(得分:0)

在ViewController中添加以下代码

 override func viewDidLoad() {
 super.viewDidLoad()

//Mark:- application move to bckground

 let backgorundNotificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector:#selector(appMovedToBackground), name: UIApplication.willResignActiveNotification, object: nil)

//Mark:- application move to foreground

let foregroundNotificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector:#selector(appMovedToForeground),name: UIApplication.willEnterForegroundNotification, object: nil)

 }

//Mark:- background method implementation

@objc func appMovedToBackground() {
print("App moved to background!")
 }

 //Mark:- foreground method implementation

@objc func appMovedToForeground() {
   print("App moved to foreground!")
}