iOS4如何重启视图控制器

发布于 2024-11-24 16:54:40 字数 506 浏览 1 评论 0原文

我想重新启动视图控制器。

我所做的是将当前视图控制器从导航堆栈中弹出,并将视图控制器的新实例推入堆栈。 然而,这是行不通的。当前视图控制器从导航堆栈中弹出,但新实例不会推送到堆栈中。

这是我的代码片段:

[[self navigationController] popViewControllerAnimated:YES];

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
[videoPlayer setMedia:media];

[[self navigationController] pushViewController:videoPlayer animated:YES];
[videoPlayer release];
videoPlayer = nil;

NSLog(@"Restarting view controller...");

知道可能出了什么问题吗?

I wish to restart a view controller.

What I've done is to pop the current view controller off the navigation stack, and push a new instance of the view controller onto the stack.
However, this does not work. The current view controller is popped off the navigation stack but, the new instance is not pushed onto the stack.

Here is my code snippet:

[[self navigationController] popViewControllerAnimated:YES];

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
[videoPlayer setMedia:media];

[[self navigationController] pushViewController:videoPlayer animated:YES];
[videoPlayer release];
videoPlayer = nil;

NSLog(@"Restarting view controller...");

Any idea what could be wrong?

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

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

发布评论

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

评论(4

云淡风轻 2024-12-01 16:54:40

根据您的代码,您似乎正在尝试弹出当前视图控制器,然后立即重新推送它,并在两个方向上都有动画。我无法想象你为什么要这样做,但暂时把它放在一边,以下是让它发挥作用的方法。

首先,将 添加到您的 @interface 声明中。然后:

- (void)repushViewController {
    self.navigationController.delegate = self;
    [[self navigationController] popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    navController.delegate = nil;
    VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
    [videoPlayer setMedia:media];

    [navController pushViewController:videoPlayer animated:YES];

    [videoPlayer release];
}

Based on your code, it looks like you're trying to pop the current view controller and then immediately re-push it, with animations in both directions. I can't imagine why you'd want to do this, but setting that aside for the moment, here's how you can make it work.

First, add <UINavigationControllerDelegate> to your @interface declaration. Then:

- (void)repushViewController {
    self.navigationController.delegate = self;
    [[self navigationController] popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    navController.delegate = nil;
    VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
    [videoPlayer setMedia:media];

    [navController pushViewController:videoPlayer animated:YES];

    [videoPlayer release];
}
划一舟意中人 2024-12-01 16:54:40

我想你需要在通话之间留出一些时间间隔。由于您正在使用动画执行此操作,因此需要一些时间才能完成第一个调用本身。

所以首先尝试,[[self navigationController] popViewControllerAnimated:NO];

I guess u need to give some time interval between the calls. Since u are doing this with animation, it would require some time to finish the first call itself.

So first try, [[self navigationController] popViewControllerAnimated:NO];.

活泼老夫 2024-12-01 16:54:40

如果您询问 VideoPlayerViewController

[[self navigationController] popViewControllerAnimated:YES];

请转到上一个视图控制器。因此当前视图控制器中的代码可能不会被执行。 VideoPlayerViewController 的新实例应在之前的视图控制器中实例化。

If you are asking about the VideoPlayerViewController,

[[self navigationController] popViewControllerAnimated:YES];

goes to the previous view controller. So code in the current view controller may not be executed. The new instance of VideoPlayerViewController should be instantiated in the previous view controller.

眼趣 2024-12-01 16:54:40

视图控制器是否被“弹出”在堆栈顶部?如果是这样,我可能会建议更改设计 - 我会使用 UINavigationController 的这种方法:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

交换视图控制器,如果您仍然需要,还可以使用动画。

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];

[videoPlayer setMedia:media];

[[self navigationController] setViewControllers:[NSArray arrayWithObject:videoPlayer]
                                      animation:YES];

[videoPlayer release];

Is, or was, the view controller being "popped" at the top of the stack? If so I might suggest a change of design - I'd use this method of UINavigationController:

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

to swap you're view controllers around, and with animation if you still require.

VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];

[videoPlayer setMedia:media];

[[self navigationController] setViewControllers:[NSArray arrayWithObject:videoPlayer]
                                      animation:YES];

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