我怎样才能观察到被呈现的,是UIViewController对象的Dismissed属性?

时间:2016-08-29 07:59:41

标签: ios objective-c iphone appdelegate key-value-observing

我想知道如何在viewController出现时知道,从UIApplication的主窗口中消失。我不想在每个UIViewController中放置代码,但是要观察UIApplication中每个viewcontroller的生命周期。

1 个答案:

答案 0 :(得分:0)

每个视图控制器都有一个生命周期。所以每个视图控制器都有独立的生命周期方法所以你必须为每个UIViewController放置代码。在app delegate中,我们检查应用程序状态。 国家是:

  1. 活跃状态
  2. 无效状态
  3. 背景状态
  4. 未运行状态
  5. 暂停状态
  6. 委托方法:

    application:didFinishLaunchingWithOptions:
    applicationWillResignActive:
    applicationDidBecomeActive:
    applicationDidEnterBackground:
    applicationWillEnterForeground:
    applicationWillTerminate:
    

    查看控制器生命周期方法:

    -(void)viewDidLoad:(BOOL)animated{
        [super viewDidLoad:animated];
    }
    
    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    
    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    }
    
    -(void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    }
    
    -(void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
    }