如何重新创建 UIViewController 堆栈?

发布于 2024-08-24 16:15:53 字数 795 浏览 6 评论 0原文

我正在为我当前的 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 UIViewControllers appeared before.

My problem is the UIViewControllers 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

jJeQQOZ5 2024-08-31 16:15:53

我不确定将“负载”功能分散到所有控制器上是实现它的最佳方法。

我宁愿将其放在应用程序的 init 中的 applicationDidFinishLauching 部分中。然后你有一个集中的地方来恢复以前的状态(恕我直言,更容易处理)。

假设您想要实现一些更高级的恢复功能,例如:

  • 显示带有活动指示器的启动 UIView,以指示用户您正在恢复以前的状态
  • 恢复导航控制器中的控制器堆栈
  • 删除启动

更容易拥有所有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:

  • displaying a splash UIView with an activity indicator to indicate the user that you're restoring previous state
  • restore the stack of your controllers in the navigation controller
  • remove the splash

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 of YES 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文