iOS如何在弹出顶视图控制器时以编程方式检测? [英] iOS how to detect programmatically when top view controller is popped?

查看:128
本文介绍了iOS如何在弹出顶视图控制器时以编程方式检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有2个视图控制器的导航控制器堆栈:VC2位于顶部,VC1位于底层。我可以在VC1中包含哪些代码来检测VC2刚刚从堆栈中弹出?

Suppose I have a nav controller stack with 2 view controllers: VC2 is on top and VC1 is underneath. Is there code I can include in VC1 that will detect that VC2 has just been popped off the stack?

因为我试图从VC1的代码中检测到VC2的弹出,所以似乎像 viewWillAppear viewDidAppear 不起作用,因为每次显示VC1时都会触发这些方法,包括首次将其推入堆栈时。

Since I'm trying to detect the popping of VC2 from within the code for VC1 it seems that something like viewWillAppear or viewDidAppear won't work, because those methods fire every time VC1 is displayed, including when it is first pushed on the stack.

编辑:看来我对原来的问题不太清楚。这是我正在尝试做的事情:确定VC1何时显示是由于VC2从堆栈顶部弹出。这是我不想做的事情:确定VC1由于被推到堆栈顶部而显示的时间。我需要某种方法来检测第一个动作但不是第二个动作。

it seems I was not very clear with my original question. Here's what I'm trying to do: determine when VC1 is being shown due to VC2 being popped off the top of the stack. Here's what I'm NOT trying to do: determine when VC1 is being shown due to being pushed onto the top of the stack. I need some way that will detect the first action but NOT the second action.

注意:我并不特别关心VC2,它可以是从堆栈中弹出的任意数量的其他VC,我关心的是当VC1变为由于其他一些VC开始从顶部突然出现堆栈的顶部。

Note: I don't particularly care about VC2, it can be any number of other VCs that get popped off the stack, what I do care about is when VC1 becomes the top of the stack again due to some other VC begin popped off the top.

推荐答案

iOS 5引入了两种新方法来处理正是这种情况。你要找的是 - [UIViewController isMovingToParentViewController] 。来自 docs

iOS 5 introduced two new methods to handle exactly this type of situation. What you're looking for is -[UIViewController isMovingToParentViewController]. From the docs:


isMovingToParentViewController

返回一个表示$的布尔值b $ b视图控制器正在被添加到父级。

Returns a Boolean value that indicates that the view controller is in the process of being added to a parent.

- (BOOL)isMovingToParentViewController

返回值

如果视图控制器出现是因为它是作为容器的子项添加的
查看控制器,否则为NO。

Return Value
YES if the view controller is appearing because it was added as a child of a container view controller, otherwise NO.

讨论

此方法仅在从内部调用时返回YES
以下方法:

Discussion
This method returns YES only when called from inside the following methods:

-viewWillAppear:

-viewDidAppear:

在您的情况下,您可以实现 -viewWillAppear :就像这样:

In your case you could implement -viewWillAppear: like so:

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

    if (self.isMovingToParentViewController == NO)
    {
        // we're already on the navigation stack
        // another controller must have been popped off
    }
}

编辑:有一个微妙的这里要考虑的语义差异 - 您是否对VC2特别是从堆栈弹出的事实感兴趣,或者您是否希望每次因为任何控制器弹出而显示VC1时收到通知?在前一种情况下,授权是一种更好的解决方案。如果你从不打算重用VC2,对VC1的直接弱引用也可以有效。

There's a subtle semantic difference to consider here—are you interested in the fact that VC2 in particular popped off the stack, or do you want to be notified each time VC1 is revealed as a result of any controller popping? In the former case, delegation is a better solution. A straight-up weak reference to VC1 could also work if you never intend on reusing VC2.

编辑2:我让这个例子更加明确通过反转逻辑而不是提前返回。

EDIT 2: I made the example more explicit by inverting the logic and not returning early.

这篇关于iOS如何在弹出顶视图控制器时以编程方式检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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