Ios PoptorootViewController不总是触发的激活视图将出现在rootViewController中

Ios PoptorootViewController不总是触发的激活视图将出现在rootViewController中,ios,objective-c,uiviewcontroller,xcode5,Ios,Objective C,Uiviewcontroller,Xcode5,我的应用程序处理打开电子邮件附件。要在我的AppDelegate中执行此操作,我调用我的专用ViewController,它是我的应用程序的RootViewController,这要归功于poptrootviewcontrolleranimated: 我在RootViewController的视图将出现和视图显示中进行了一些处理,但如果切换到邮件应用程序之前显示的视图控制器是我的RootViewController,则不会调用这些处理 案例1:通常的viewWillAppease/viewDid

我的应用程序处理打开电子邮件附件。要在我的
AppDelegate
中执行此操作,我调用我的专用ViewController,它是我的应用程序的
RootViewController
,这要归功于
poptrootviewcontrolleranimated:

我在RootViewController的
视图将出现
视图显示
中进行了一些处理,但如果切换到邮件应用程序之前显示的视图控制器是我的RootViewController,则不会调用这些处理

案例1:通常的viewWillAppease/viewDidAppease方法不被调用: (RootViewController->切换到邮件应用程序->在我的应用程序中打开附件->在AppDelegate PoptorootViewController中激活->RootViewController)

案例2:通常的viewWillAppease/viewDidAppease方法被调用: (OtherViewController->切换到邮件应用程序->在我的应用程序中打开附件->在AppDelegate PoptorootViewController中激活->RootViewController)

在internet上发现此技巧,但在案例1中不起作用:


我如何解决这个问题,并始终调用常规的viewwilldisease/viewdidedisease方法?

听起来像是因为在案例1中,在应用程序的上下文中,您的RootViewController已经“可见”,因此
popToRootViewController
无需执行任何操作。

视图将出现:
视图显示:
仅当发生与应用程序相关的情况时才会调用方法。在应用程序之间切换时不会调用这些方法。您应该依赖appDelegate中的
应用程序WillEnterForeground:
应用程序IDBecomeActive:
方法

还有一件事需要注意的是,当你

RootViewController->poptrootviewcontrolleranimated->RootViewController

您的根视图控制器的视图已经可见,因此它不会触发
视图将显示:
视图显示:
方法

我建议你做类似的事情

RootViewController.m

-(void)viewDidAppear:(BOOL)animated
{
   [self doSomething];
}

-(void)doSomething
{
   //Your functionality
}

AppDelegate.m

- (void)applicationDidBecomeActive:(UIApplication *)application
{
   [self.window.rootViewController doSomething];
}

您还可以将根视图控制器作为侦听器添加到
UIApplicationIDBecMeactiveNotification
UIApplicationWillEnterForegroundNotification

您的视图控制器已可见,因此通常不会调用ViewWillDisplay

如果您想知道用户何时返回应用程序,只需在RootViewController init方法中添加以下代码:

  [[NSNotificationCenter defaultCenter] addObserver:self 
                                       selector:@selector(willEnterForeground:)
                                           name: UIApplicationWillEnterForegroundNotification
                                         object:nil];
实现你想要的一切

- (void)willEnterForeground:(NSNotification *)notification
不要忘记在释放RootViewController时停止观察:

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

您的解决方案听起来不错,但如何知道何时解除分配
RootViewController
?当
direceivememorywarning
方法被激发时?