iPhone UINavigationController 后退按钮丢失
目前我正在为 iPhone 创建一个基于导航的应用程序,其中 NavigationController 的根视图是主菜单(主页)。还有另外 3 个视图(名为 View1、View2 和 View3),可以通过这种方式访问;主菜单->查看1->视图2->视图3。
每个视图都有一个“主页按钮”,它将调用“popToRootViewControllerAnimated”并返回主菜单。在 View2 中按下此按钮时,将返回主菜单。但是,当您从主菜单转到 View1 时,“后退”按钮将会丢失。这是唯一遇到此问题的视图。
造成这种情况的原因是我们“延迟初始化”View1。换句话说,我们在主菜单中保留对 View1 的引用,以便我们随时可以使用它(以保持该视图的状态)。
奇怪的是,这个问题只有在按 View2 中的“Home 按钮”时才会发生。使用 View3 就没有问题。
在每个视图的“ViewWillAppear”方法中,我们有以下代码,将向视图的导航栏添加后退按钮;
[self.navigationItem.backBarButtonItem release];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
有谁知道如何解决这个问题?
In am currently creating a Navigation-based application for the iPhone, where the root view of the NavigationController is the main menu (home). There are 3 other views (named View1, View2 and View3), which can be accessed in this manner; main menu -> View1 -> View2 -> View3.
Each of the Views has a 'Home button' that will call 'popToRootViewControllerAnimated' and return to the main menu. When this button is pressed in View2, it returns to the main menu. However, when you go from the main menu to View1, the Back button will be missing. This is the only View that suffers from the problem.
The thing that has brought this around is that we are 'lazily initialising' View1. In other words, we are keeping a reference to View1 in the main menu so that we have it constantly at hand (to keep the state of that view).
The weird thing is that this problem only happens by pressing the 'Home button' from View2. With View3 there is no issue.
In each of the views' 'ViewWillAppear' methods, we have the following code that will add a back button to the view's NavigationBar;
[self.navigationItem.backBarButtonItem release];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Does anyone have any idea on how to remedy this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在前一个视图控制器中应用此代码,因为后退按钮取决于导航堆栈中可见视图控制器上方的视图控制器。
You must apply this code in the previous view controller, as the back button depends on the view controller above the visible view controller in the navigation stack.