UiViewController - 弹出不弹出

发布于 2024-12-05 19:17:45 字数 644 浏览 0 评论 0原文

当从另一个 UIViewController 按下按钮时,我显示 UIControllerView 子类,如下所示:

- (IBAction)openNextLevelViewController
{
    NSLog(@"openNextlevelViewController");

    [self.navigationController pushViewController:nextLevelViewController animated:YES];

}

应用程序将从触发此方法的按钮按下时从该视图返回:

-(IBAction) returnToStart {

    NSLog(@"returnToStart method called");
    [self.navigationController popViewControllerAnimated:YES];

}

问题是显示的视图不会在弹出窗口上被销毁/解除分配。因此,当它被推送时,它不会执行 viewDidLoad,它会启动一些变量。这可能会导致相关问题,即当用户第二次按下返回按钮时,“弹出”不再导致返回到前一个控制器。

处理这个问题的最佳方法是什么?我可以将初始化代码移至“willAppear”方法,但似乎几乎可以随机调用它。

I'm displaying UIControllerView subclass when a button is pressed from a another UIViewController like this:

- (IBAction)openNextLevelViewController
{
    NSLog(@"openNextlevelViewController");

    [self.navigationController pushViewController:nextLevelViewController animated:YES];

}

And the app will return from that view on a button push which trigger this method:

-(IBAction) returnToStart {

    NSLog(@"returnToStart method called");
    [self.navigationController popViewControllerAnimated:YES];

}

The problem is that the displayed view not getting destroyed/deallocated on the pop. As a result, when it gets pushed, it's not executing the viewDidLoad, which initiates some variables. This may be causing a related problem where, the second time through, when the user presses the return button, the "pop" no longer causes a return to the previous controller.

What's the best way to deal with this? I could move the initialization code to the "willAppear" method, but it seems as if that could be called almost randomly.

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

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

发布评论

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

评论(1

梦萦几度 2024-12-12 19:17:45

好吧,它没有被释放,因为 nextLevelViewController 仍然被保留在其他地方。最有可能在你的 nextLevelViewController 变量中。

- (IBAction)openNextLevelViewController
{
    NSLog(@"openNextlevelViewController");
    // assuming you have nib already set up
    UIViewController *nextLevelViewController = [[NextLevelViewController alloc] init]; 
    // RETAIN COUNT = 1

    // navigationController retains your controller
    [self.navigationController pushViewController:nextLevelViewController animated:YES]; 
    // RETAIN COUNT = 2

    // Don't need it any more let navigation controller handle it.
    [nextLevelViewController release]
    // RETAIN COUNT = 1 (by NavigationController)
}

现在

-(IBAction) returnToStart {
    [self.navigationController popViewControllerAnimated:YES];
    // RETAIN COUNT = 0, dealloc will be called on your viewController, make sure to release all your retained objects.
}

,当您的控制器弹出时,它应该被释放(不应该保留在其他地方)。下次您调用 openNExtLevelViewController 时,它无论如何都会初始化 viewController 的新实例。

我喜欢在不再需要(显示)时释放 viewController,而不是将其保留在内存中。尽可能让 navigationController 和 TabBarController 处理 viewController。

Well, its not getting released because nextLevelViewController is still being retained somewhere else. Most likely in your nextLevelViewController variable.

- (IBAction)openNextLevelViewController
{
    NSLog(@"openNextlevelViewController");
    // assuming you have nib already set up
    UIViewController *nextLevelViewController = [[NextLevelViewController alloc] init]; 
    // RETAIN COUNT = 1

    // navigationController retains your controller
    [self.navigationController pushViewController:nextLevelViewController animated:YES]; 
    // RETAIN COUNT = 2

    // Don't need it any more let navigation controller handle it.
    [nextLevelViewController release]
    // RETAIN COUNT = 1 (by NavigationController)
}

Further On

-(IBAction) returnToStart {
    [self.navigationController popViewControllerAnimated:YES];
    // RETAIN COUNT = 0, dealloc will be called on your viewController, make sure to release all your retained objects.
}

Now when your controller gets popped, it SHOULD get released (shouldn't have been retained anywhere else). And next time you call openNExtLevelViewController, it'll be initializing a new instance of your viewController anyway.

I'm a fan of releasing viewController when it is no longer needed (displayed), instead of holding it in memory. Let navigationController and TabBarController handle viewControllers whenever possible.

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