UINavigationController 不会完全推送视图,仅将导航工具栏更改为下一个视图的工具栏
所以我有一个 iPhone 应用程序,它利用 UINavigationController 来设置视图。当应用程序首次启动时,它向用户呈现一个 UITableViewController,用户可以选择一个项目,然后它将推送另一个视图。现在我已经设置了它,以便我的应用程序记住用户的最后选择并自动选择它并加载正确的视图控制器。唯一的问题是,当我自动加载下一个视图时,我遇到了一个非常奇怪的故障。
当推动视图时,导航工具栏将发生变化,以便显示指向上一个视图的后退按钮,但不会显示下一个视图。相反,它会继续显示表格视图,我也可以与它交互。我可以按后退按钮,它会将工具栏更改回来,并且表格视图仍然显示。然后,当我选择一个项目时,它会很好地加载视图。
感谢您的帮助。
代码:
我根据是否可以连接到服务器来确定是否推送视图控制器。我在后台线程中执行此操作:
- (void)startingThread
{
[NSThread detachNewThreadSelector:@selector(loginThread:) toTarget:self withObject:communicator];
}
- (void)loginThread:(MowerCommunicator *)communicator
{
//If it can connect, launch thread complete.
[self performSelectorOnMainThread:@selector(loginThreadComplete:) withObject:communicator waitUntilDone:NO];
}
- (void)loginThreadComplete:(MowerCommunicator *)communicator
{
//push view controller
}
现在我添加了 NSLog 语句来跟踪视图是否确实“显示”并且 viewWillAppear 和 viewDidAppear 都被调用。我还检查了导航控制器的委托方法,它们也被调用。
我有一个视图,它是初始启动视图,它从服务器读取以确定在下一个表视图中显示的内容。这被推得很好,当桌面视图被推时,我隐藏后退按钮,这样用户在不关闭应用程序的情况下无法返回到第一个视图。然后表视图查看 NSUserDefaults 中的变量以确定是否存在已保存的索引,然后推送下一个视图控制器。这就是故障发生的时候。如果我然后按后退按钮“返回”表视图(这实际上只是更改导航工具栏),然后我从表视图中选择一个项目,它会正确加载下一个视图。此外,当用户从表视图中按下某个项目以及应用程序自动加载视图时,我会调用完全相同的方法。
So I have an iPhone application that utilizes a UINavigationController for setting up the views. When the application first starts, it presents the user with a UITableViewController and the user can select an item and it will push another view. Now I have it set so that my app remembers the user's last selection and automatically selects it and loads the correct view controller. The only problem is that I am experiencing a really weird glitch when I load the next view automatically..
When the view is pushed, the navigation toolbar will change so that a back button directed to the previous view is showing but it won't display the next view. It will instead keep showing the table view and I can interact with it as well. I can press the back button and it will change the toolbar back and the tableview is still shown. Then when I select an item it loads the view just fine.
Thanks for the help.
Code:
I determining whether to push the view controller based on whether it can connect to a server. I do this in a backround thread:
- (void)startingThread
{
[NSThread detachNewThreadSelector:@selector(loginThread:) toTarget:self withObject:communicator];
}
- (void)loginThread:(MowerCommunicator *)communicator
{
//If it can connect, launch thread complete.
[self performSelectorOnMainThread:@selector(loginThreadComplete:) withObject:communicator waitUntilDone:NO];
}
- (void)loginThreadComplete:(MowerCommunicator *)communicator
{
//push view controller
}
Now I have added NSLog statements to track if the view is actually "showing" and both viewWillAppear and viewDidAppear get called. I also check the delegate methods for the navigation controller and they get called as well.
I have a view that is the initial startup view and it reads from a server to determine what to display in the next table view. That gets pushed fine and when the tableview gets pushed, I hide the back button so the user can't get back to the first view without closing the app. Then the tableview looks at a variable in NSUserDefaults to determine with there is a saved index and then pushes the next view controller. That is when the glitch occurs. If I then press the back button to "go back" to the table view (this really just changes the navigation toolbar) and then I select an item from the table view, it loads the next view correctly. Also, I call the exact same methods when the user pressed an item from the table view and when the app loads the view automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解释是,您将初始的 tableviewcontroller 两次推送到导航控制器的堆栈上。
如果您将初始 tableviewcontroller 设置为笔尖中导航的第一个控制器,但随后在尝试自动显示堆叠视图时将相同的控制器推入堆栈,您将得到您所描述的内容。导航从笔尖的控制器开始,然后将另一个实例推到其上。
您应该在执行自动推送之前和之后记录导航的
viewControllers
属性,以查看堆栈的实际状态。The simplest explanation is that you're pushing the initial tableviewcontroller onto the navigation controller's stack twice.
If you have the initial tableviewcontroller set as the nav's first controller in a nib but then you push the same controller onto the stack when trying to automatically display the stacked views, you would get what you are describing. The nav starts out with the controller from the nib and then you push another instance on top of that.
You should log the nav's
viewControllers
property before and after you do the automatic push to see what the actual state of the stack.