如何重新创建 UIViewController 堆栈?
我正在为我当前的 iPhone 应用程序编写一个“加载”功能。
实际上我想做的就是让用户回到他/她离开的地方。为此,我想重新创建之前出现的所有 UIViewController
。
我的问题是,如果我简单地调用 [[self navigationController] PushViewController:newControllerAnimated:YES]; ,则
UIViewController
不会出现。
当作为事件处理程序调用时(例如触摸按钮后),相同的代码可以正常工作。但是,如果我在没有偶数的情况下执行相同操作(例如在 viewDidLoad
中),则新视图控制器的 loadView
方法将不会被调用。
我的实际代码(带有一些伪元素):
- (void)viewDidLoad {
[super viewDidLoad];
if (loading)
[self onSomeEvent];
}
- (void)onSomeEvent {
UIViewController *newController = //init....;
[[self navigationController] pushViewController:newController animated:YES];
[newController release];
}
我猜 viewDidLoad
不是进行此类调用的正确位置,但那又是什么呢?
I'm writing a 'load' feature for my current iPhone app.
Practically what I want to do is get the user back where s/he left off. To do so I want to recreate all the UIViewController
s appeared before.
My problem is the UIViewController
s won't appear if I simply call [[self navigationController] pushViewController:newController animated:YES];
.
The same code works fine when invoked as an event handler, like after touching a button. But if I do same without an even (for ex. in viewDidLoad
) the new view controller's loadView
method won't get called.
My actual code (with some pseudo elements):
- (void)viewDidLoad {
[super viewDidLoad];
if (loading)
[self onSomeEvent];
}
- (void)onSomeEvent {
UIViewController *newController = //init....;
[[self navigationController] pushViewController:newController animated:YES];
[newController release];
}
I guess viewDidLoad
is not the right place to do such a call, but then what is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定将“负载”功能分散到所有控制器上是实现它的最佳方法。
我宁愿将其放在应用程序的 init 中的
applicationDidFinishLauching
部分中。然后你有一个集中的地方来恢复以前的状态(恕我直言,更容易处理)。假设您想要实现一些更高级的恢复功能,例如:
更容易拥有所有applicationDidFinishLauching 中的这段代码:它全部在某一点进行管理。
此外,在将旧状态恢复到导航控制器时,您可以避免使用转换,并在推送控制器时使用
animated:NO
而不是YES
。从您的角度来看,这将更容易处理,并且如果您删除实现转换所需的时间,恢复时间可能会减少。I'm not sure that spreading your "load" feature all accross your controllers is the best way to achieve it.
I would rather put it in the init of your application, in the
applicationDidFinishLauching
part. Then you have a centralized place where you restore the previous state (easier to handle IMHO).Let's suppose you want to implement some more advanced restore feature like:
it's easier to have all this code in the applicationDidFinishLauching : it's all managed at one point.
Morever, when restoring the old state to your navigation controller, you can avoid using the transitions and use
animated:NO
instead ofYES
when pushing your controllers. It will be easier to handle from your perspective and the restore time may be decreased if you remove time needed to achieve transitions.