在我的viewDidAppear中,我如何知道孩子什么时候被解开? [英] In my viewDidAppear, how do I know when it's being unwound by a child?
问题描述
当我的孩子执行展开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)
注意:孩子正在向第一个视图控制器展开,所以这是一个中间视图控制器,而不是真正的root。
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被调用不同,那么你需要添加一些代码来检测是否发生了展开。要执行此操作,您可以执行以下操作:
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.
}
}
希望这符合您的要求。
有关处理展开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屋!