从后台打开应用程序时不调用ViewDidAppear

Zoh*_*aib 167 iphone xcode objective-c ios swift

我有一个View Controller,其值为0(标签),当我从另一个打开View Controller时,ViewController我设置viewDidAppear为在标签上设置值20.它工作正常,但是当我关闭我的应用程序,比再次我打开我的应用程序,但价值不因为改变viewDidLoad,viewDidAppearviewWillAppear没有被调用.打开我的应用程序时如何打电话.我必须做什么applicationDidBecomeActive吗?

dan*_*anh 307

对事件的确切顺序感到好奇,我按如下方式对应用程序进行了检测:(@ Zohaib,您可以使用下面的NSNotificationCenter代码来回答您的问题).

// AppDelegate.m

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"app will enter foreground");
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"app did become active");
}

// ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)appDidBecomeActive:(NSNotification *)notification {
    NSLog(@"did become active notification");
}

- (void)appWillEnterForeground:(NSNotification *)notification {
    NSLog(@"will enter foreground notification");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"view will appear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"view did appear");
}
Run Code Online (Sandbox Code Playgroud)

在启动时,输出如下所示:

2013-04-07 09:31:06.505 myapp[15459:11303] view did load
2013-04-07 09:31:06.507 myapp[15459:11303] view will appear
2013-04-07 09:31:06.511 myapp[15459:11303] app did become active
2013-04-07 09:31:06.512 myapp[15459:11303] did become active notification
2013-04-07 09:31:06.517 myapp[15459:11303] view did appear
Run Code Online (Sandbox Code Playgroud)

输入背景然后重新进入前景:

2013-04-07 09:32:05.923 myapp[15459:11303] app will enter foreground
2013-04-07 09:32:05.924 myapp[15459:11303] will enter foreground notification
2013-04-07 09:32:05.925 myapp[15459:11303] app did become active
2013-04-07 09:32:05.926 myapp[15459:11303] did become active notification
Run Code Online (Sandbox Code Playgroud)

  • 这是一个非常有用的答案.我做了一个Swift版本[这里](http://stackoverflow.com/a/34529572/3681880). (4认同)

nsg*_*ver 131

使用Objective-C

您应该UIApplicationWillEnterForegroundNotification在自己ViewControllerviewDidLoad方法中注册一个,每当应用程序从后台返回时,您可以在注册通知的方法中执行任何操作.ViewControllerviewWillAppear中viewDidAppear当应用程序回来,从背景到前景将不会被调用.

-(void)viewDidLoad{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff)

  name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)doYourStuff{

   // do whatever you want to do when app comes back from background.
}
Run Code Online (Sandbox Code Playgroud)

不要忘记取消注册您注册的通知.

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您注册了viewControllerfor,UIApplicationDidBecomeActiveNotification那么每次您的应用程序变为活动状态时都会调用您的方法,不建议您注册viewController此通知.

使用Swift

要添加观察者,您可以使用以下代码

 override func viewDidLoad() {
    super.viewDidLoad()

     NSNotificationCenter.defaultCenter().addObserver(self, selector:"doYourStuff", name:
     UIApplicationWillEnterForegroundNotification, object: nil)
 }

 func doYourStuff(){
     // your code
 }
Run Code Online (Sandbox Code Playgroud)

要删除观察者,您可以使用swift的deinit函数.

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)

  • 与http://stackoverflow.com/a/15854893/341994 :)有着惊人的相似之处:) (5认同)
  • 是的它是:)有时很难找到答案:) (4认同)

Fan*_*ing 43

Swift 3.0 ++版本

在您的viewDidLoad通知中心注册,以收听从后台操作打开的内容

NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
Run Code Online (Sandbox Code Playgroud)

然后添加此功能并执行所需的操作

func doSomething(){
    //...
}
Run Code Online (Sandbox Code Playgroud)

最后添加此函数以在视图控制器被销毁时清理通知观察器.

deinit {
    NotificationCenter.default.removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)


geb*_*bel 10

斯威夫特4.2。版

向NotificationCenter注册以在viewDidLoad应用程序从后台返回时收到通知

NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: UIApplication.willEnterForegroundNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

实现应调用的方法。

@objc private func doSomething() {
    // Do whatever you want, for example update your view.
}
Run Code Online (Sandbox Code Playgroud)

ViewController摧毁后,您可以删除观察者。仅在iOS9和macOS 10.11以下才需要

deinit {
    NotificationCenter.default.removeObserver(self)
}
Run Code Online (Sandbox Code Playgroud)