如何让视图控制器运行更新代码,当它被带到视图堆栈的顶部? [英] How do I have a view controller run updating code when it is brought to the top of the stack of views?

查看:119
本文介绍了如何让视图控制器运行更新代码,当它被带到视图堆栈的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个viewController(Planner),当应用程序启动时加载两个视图控制器(InfoEditor和MonthlyPlan)。 MonthlyPlan隐藏在InfoEditor后面(加载时)。

I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when the application starts. MonthlyPlan is hidden behind InfoEditor (on load).

所以我的问题是当我交换InfoEditor for MonthlyPlan(MonthlyPlan被带到顶部)每月平均视图更新。

So my question is when I exchange InfoEditor for MonthlyPlan (MonthlyPlan gets brought to the top) how can I have data on the MonthlyPlan view be updated. An NSLog in viewDidLoad is being called when the application starts (which makes sense.) NSLogs in viewDidAppear and viewWillAppear aren't doing anything.

任何想法?

感谢!

- 添加更多详情 -

-- Adding more details --

我自己创建视图层次。一个简单的viewController,只是加载另外两个viewController。同时加载两个子viewControllers(在启动应用程序时)。要交换这两个视图,我使用的代码如下:

I'm creating the view hierarchy myself. A simple viewController that is just loading two other viewControllers. The two child viewControllers are loaded at the same time (on launch of application.) To exchange the two views I'm using this code:

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

交换视图是正常的。缺少的部分只是告诉子视图的一些方法,你在前面,更新一些属性。

The exchanging of the views is fine. The part that is missing is just some way of telling the subview, you're in front, update some properties.

推荐答案

这里缺乏细节。你如何交换这两个视图?

There's a lack of details here. How are you "exchanging" the two views?

如果你使用UINavigationController作为容器,那么当你push / pop一个新的viewController时,viewWillAppear / viewDidAppear将被调用。这些调用由UINavigationController本身完成。如果你使用UINavigationController,那么确保你的原型正确的这些函数。

If you were using a UINavigationController as the container then viewWillAppear/viewDidAppear would be called whenever you push/pop a new viewController. These calls are made by the UINavigationController itself. If you ARE using a UINavigationController then make sure you have the prototypes correct for these functions.

- (void)viewWillAppear:(BOOL)animated

如果您试图自己实现视图层次,那么您可能需要自己作为激活/停用视图的一部分。从viewWillAppear的SDK页面;

If you are trying to implement a view hierarchy yourself then you may need to make these calls yourself as part of activating/deactivating the views. From the SDK page of viewWillAppear;


如果属于视图
控制器的视图添加到视图
层次结构,视图
控制器将不会收到这个
消息。如果在视图层次结构中插入或添加视图
,并且它有一个
视图控制器,则应该直接发送
关联的视图控制器这个
消息。

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

更新:

清除:这是一种情况,您必须根据SDK建议自己发送消失/显示消息。当视图被直接插入/删除/改变时,这些函数不被自动调用,它们由提供层次结构支持的高级代码(例如UINavigationController)使用。

With the new details the problem is clear: This is a situation where you must send the disappear/appear messages yourself as suggested by the SDK. These functions are not called automagically when views are directly inserted/removed/changed, they are used by higher-level code (such as UINavigationController) that provides hierarchy support.

你认为你的使用exchangeSubView的例子,然后没有什么消失,一个视图恰好完全或部分覆盖其他地区和不透明度。

If you think about your example of using exchangeSubView then nothing is disappearing, one view just happens to cover the other wholly or partially depending on their regions and opacity.

我建议如果你想交换视图,那么你真的需要删除/添加,并手动发送viewWillAppear / viewWillDisappear通知给他们的控制器。

I would suggest that if you wish to swap views then you really do remove/add as needed, and manually send the viewWillAppear / viewWillDisappear notifications to their controllers.

例如

// your top level view controller
-(void) switchActiveView:(UIViewController*)controller animated:(BOOL)animated
{
    UIController* removedController = nil;

    // tell the current controller it'll disappear and remove it
    if (currentController)
    {
        [currentController viewWillDisapear:animated];
        [currentController.view removeFromSuperView];
        removedController = currentController;
    }

    // tell the new controller it'll appear and add its view
    if (controller)
    {
        [controller viewWillAppear:animated];
        [self.view addSubView:controller.view];
        currentController = [controller retain];
    }

    // now tell them they did disappear/appear
    [removedController viewDidDisappear: animated];
    [currentController viewDidAppear: animated];
    [removedController release];
}

这篇关于如何让视图控制器运行更新代码,当它被带到视图堆栈的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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