UINavigation 控制器 Push 和 Pop

发布于 2024-11-26 10:18:43 字数 423 浏览 1 评论 0原文

单击自定义后退按钮后,我必须从当前视图控制器弹出到 HomeScreenViewController,该按钮作为灯箱添加在主窗口上。我使用了以下代码:

HomeScreenViewController *homeController = [[HomeScreenViewController alloc] 
initWithNibName:@"HomeScreenViewController" bundle:nil];
    [self.navigationController popToViewController:homeController animated:YES];
    [homeController release];

我因异常而崩溃:尝试弹出到不存在的视图控制器。

如何实现?实施它需要哪些改变?

I have to pop to HomeScreenViewController from current view controller after clicking on custom back button, which is added on main window as a light box. I used following code :

HomeScreenViewController *homeController = [[HomeScreenViewController alloc] 
initWithNibName:@"HomeScreenViewController" bundle:nil];
    [self.navigationController popToViewController:homeController animated:YES];
    [homeController release];

I got crashing with exception : Tried to pop to a view controller that doesn't exist.

How can it be implemented? What changes are required for implementing it?

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

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

发布评论

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

评论(3

江心雾 2024-12-03 10:18:43

显然,您正在创建导航堆栈上不存在的 HomeScreenViewController 的新实例。您必须获取现有实例并将其用作 popToViewController:animated: 方法的参数。您可以通过从 viewControllers 数组获取视图控制器来完成此操作,该数组是 UINavigationController 上的一个属性。它们按顺序索引,因此如果视图控制器位于索引 1,则使用获取视图控制器。

UIViewController * viewController = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:viewController animated:YES];

如果您想返回根视图控制器,请改用 popToRootViewControllerAnimated:

Clearly you are creating a new instance of HomeScreenViewController which doesn't exist on the navigation stack. You will have to get the existing instance and use it as an argument for popToViewController:animated: method. You can do it by getting the view controller from the viewControllers array which is a property on UINavigationController. They are indexed in order so if the view controller is at index 1 then get the view controller using

UIViewController * viewController = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:viewController animated:YES];

If you want to go back to root view controller, use popToRootViewControllerAnimated: instead.

眼睛会笑 2024-12-03 10:18:43

试试这个朋友

[self.navigationController popToRootViewControllerAnimated:YES/NO];

Try this friend

[self.navigationController popToRootViewControllerAnimated:YES/NO];
做个ˇ局外人 2024-12-03 10:18:43

另外,不要忘记 AppDelegate (.m)

navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

self.navigationController = navigationController;
self.window.rootViewController = self.navigationController;

[self.window makeKeyAndVisible];

和 AppDelegate (.h)

@property (strong, nonatomic) UINavigationController *navigationController;

中的这段代码:当您在应用程序中自定义很多内容时,有时可能会出现问题。

Also don't forget about this code in your AppDelegate (.m):

navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

self.navigationController = navigationController;
self.window.rootViewController = self.navigationController;

[self.window makeKeyAndVisible];

and in AppDelegate (.h)

@property (strong, nonatomic) UINavigationController *navigationController;

It can be problem sometimes, when you customizing a lot things in your app.

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