在我的 viewDidAppear 中,我怎么知道它何时被孩子解开? [英] In my viewDidAppear, how do I know when it's being unwound by a child?

查看:27
本文介绍了在我的 viewDidAppear 中,我怎么知道它何时被孩子解开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的孩子执行 unwind segue 时,我的控制器的 viewDidAppear 被调用.

When my child performs an unwind segue, my controller's viewDidAppear gets called.

在这个方法中(仅这个方法,我需要知道它是否来自放松)

In this method (and this method alone, I need to know whether it was from an unwind or not)

注意:孩子正在展开到第一个视图控制器,所以这是一个中级视图控制器,而不是真正的根.

Note: the child is unwinding to the very first view controller, so this is an intermediate view controller, not the true root.

推荐答案

您应该能够使用以下内容在每个控制器中检测视图控制器的暴露是由于被推送/呈现,还是作为由于弹出/关闭/放松而被暴露的结果.

You should be able to use the following to detect in each controller if the exposure of the view controller was as a result of being pushed/presented, or as a result of being exposed as a result of pop/dismiss/unwind.

这可能或可能足以满足您的需求.

This may or may be enough for your needs.

- (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    // Handle controller being exposed from push/present or pop/dismiss
    if (self.isMovingToParentViewController || self.isBeingPresented){
        // Controller is being pushed on or presented.
    }
    else{
        // Controller is being shown as result of pop/dismiss/unwind.
    }
}

如果您想知道 viewDidAppear 被调用是因为 unwind segue 与被调用的传统 pop/dismiss 不同,那么您需要添加一些代码来检测是否发生了 unwind.为此,您可以执行以下操作:

If you want to know that viewDidAppear was called because of an unwind segue as being different from a conventional pop/dismiss being called, then you need to add some code to detect that an unwind happened. To do this you could do the following:

对于任何您想要检测纯展开的中间控制器,添加以下形式的属性:

For any intermediate controller you want to detect purely an unwind in, add a property of the form:

/** BOOL property which when TRUE indicates an unwind occured. */
@property BOOL unwindSeguePerformed;

然后重写 unwind segue 方法 canPerformUnwindSegueAction:fromViewController:withSender: 方法如下:

Then override the unwind segue method canPerformUnwindSegueAction:fromViewController:withSender: method as follows:

- (BOOL)canPerformUnwindSegueAction:(SEL)action
                 fromViewController:(UIViewController *)fromViewController
                         withSender:(id)sender{
  // Set the flag indicating an unwind segue was requested and then return
  // that we are not interested in performing the unwind action.
  self.unwindSeguePerformed = TRUE;

  // We are not interested in performing it, so return NO. The system will
  // then continue to look backwards through the view controllers for the 
  // controller that will handle it.
  return NO;
}

现在您有一个标志来检测展开,并有一种方法可以在展开之前检测到展开.然后调整 viewDidAppear 方法以包含此标志.

Now you have a flag to detect an unwind and a means to detect the unwind just before it happens. Then adjust the viewDidAppear method to include this flag.

- (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    // Handle controller being exposed from push/present or pop/dismiss
    // or an unwind
    if (self.isMovingToParentViewController || self.isBeingPresented){
        // Controller is being pushed on or presented.
        // Initialize the unwind segue tracking flag.
        self.unwindSeguePerformed = FALSE;
    }
    else if (self.unwindSeguePerformed){
        // Controller is being shown as a result of an unwind segue
    }
    else{
        // Controller is being shown as result of pop/dismiss.
    }
}

希望这能满足您的要求.

Hopefully this meets your requirement.

有关处理 unwind segue 链的文档,请参阅:https://developer.apple.com/library/ios/technotes/tn2298/_index.html

For docs on handling the unwind segue chain see: https://developer.apple.com/library/ios/technotes/tn2298/_index.html

这篇关于在我的 viewDidAppear 中,我怎么知道它何时被孩子解开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆