iOS:isMovingToParentViewController无法正常工作 [英] iOS: isMovingToParentViewController is not working as expected
问题描述
我的设置很简单,我的问题与这一个。然而,为了更好地解释它我在这里发布:
My setup is simple, and my issue is not very different from this one. However to better explain it I have posted it here:
NavController -> VC1 -> VC2
VC1是NavController的根视图控制器。
VC2可通过VC1的Push segue访问。
VC1 is root view controller of NavController. VC2 is accessible via Push segue from VC1.
我想在VC1中检测是否:
I want to detect, within VC1, whether:
它直接作为根视图控制器出现(通过Push)
它出现了VC2被弹出的结果
It appeared directly as root view controller (via Push) It appeared as a result of VC2 being popped
我读了下面应该说的文档告诉我以后是否属实。
I read the docs which says following should tell me if later is true.
isMovingToParentViewController == NO
然而情况并非如此,以上情况总是证明是正确的。
这意味着,(self.isMovingToParentViewController == NO)总是在发生。
However that is not the case, and above condition ALWAYS turns out to be TRUE. Which means that, (self.isMovingToParentViewController == NO) is always happening.
这是我的代码:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
//pushed to stack
if (self.isMovingToParentViewController == YES)
{
//First time
}
else
//popped off
{
//via Pop from VC2
}
}
同样是viewDidAppear的情况。
Same is the case for viewDidAppear, too.
事实上检查,我在开始时设置了断点,在以下两种情况下检查以下所有都是FALSE:
For a matter of fact check, I put breakpoint at the start, and checked that all of the following are FALSE, in both cases:
([self isMovingFromParentViewController])
([self isMovingToParentViewController])
([self isBeingPresented])
([self isBeingDismissed])
发生了什么事?
我的故事板里有什么东西可以搞定吗?
请帮忙...
What is happening? Is there anything I goofed up in my storyboard? Please help...
推荐答案
不幸的是,isMovingToParentViewController对于根视图控制器不是真的,所以我通常使用BOOL处理这种情况,
Unfortunately, isMovingToParentViewController isn't true for the root view controller, so I usually handle this situation with a BOOL,
@implementation ViewController {
BOOL isFirstAppearance;
}
- (void)viewDidLoad
{
[super viewDidLoad];
isFirstAppearance = YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (isFirstAppearance) {
NSLog(@"root view controller is moving to parent");
isFirstAppearance = NO;
}else{
NSLog(@"root view controller, not moving to parent");
}
}
这篇关于iOS:isMovingToParentViewController无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!