每次我将视图控制器压入堆栈时,如何从 appDelegate 重新加载视图控制器?

发布于 2024-10-10 23:26:39 字数 541 浏览 3 评论 0原文

目前,我的应用程序有一个导航屏幕,允许用户选择其他视图。单击时,navController 只需推送相关的特定视图控制器。如果我再也没有回到相同的视图并期望它被重新加载,那么这非常有用。

在下面的代码中,我根据请求推送视图控制器。

- (void)optionClicked:(NSString *)optionName
{
  if ([@"First" isEqualToString:optionName]) {
    [navController pushViewController:firstController animated:YES];
  } else if ([@"Next" isEqualToString:optionName]) {
    [navController pushViewController:nextController animated:YES];
  }
}

完成视图后,我只需将其从堆栈中弹出即可。但是,下次用户从菜单中选择相同的选项时,它不会“干净”加载,这就是我的问题所在。每次将视图控制器推入堆栈时,如何加载新的视图控制器?

Currently my application has a single navigation screen that allows the user to select other views. When clicked the navController simply does a push of the specific view controller in question. This works great if I never come back to the same view again expecting it to be reloaded.

In the code below I push a view controller when requested.

- (void)optionClicked:(NSString *)optionName
{
  if ([@"First" isEqualToString:optionName]) {
    [navController pushViewController:firstController animated:YES];
  } else if ([@"Next" isEqualToString:optionName]) {
    [navController pushViewController:nextController animated:YES];
  }
}

When done with a view I simply pop it from the stack. But the next time a user selects this same option from the menu it's not loaded "clean" and this is where my question comes in. How can I load a view controller fresh each time it's pushed on the stack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

街角卖回忆 2024-10-17 23:26:39

您必须重新初始化视图控制器。

  if ([@"First" isEqualToString:optionName]) {
    if (firstController) 
    { [firstController release]; } // assuming you've got a retain on it.
    firstController = [[MyViewControllerSubclass alloc] init];
    [navController pushViewController:firstController animated:YES];
  } 

在这种情况下,我建议使用带有保留的属性。 @property (nonatomic, keep) MyViewControllerSubclass *firstController;

这样你就可以使用 self.firstController = [[[MyViewControllerSubclass alloc] init] autorelease]; 内存管理是大部分是为你做的。 (尽管你仍然需要在 dealloc 中释放。)

You have to reinitialize the viewcontroller.

  if ([@"First" isEqualToString:optionName]) {
    if (firstController) 
    { [firstController release]; } // assuming you've got a retain on it.
    firstController = [[MyViewControllerSubclass alloc] init];
    [navController pushViewController:firstController animated:YES];
  } 

In this situation I would suggest using a property with retain on it. @property (nonatomic, retain) MyViewControllerSubclass *firstController;

that way you can use self.firstController = [[[MyViewControllerSubclass alloc] init] autorelease]; and the memory management is mostly done for you. (Although you still have to release in dealloc.)

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