如何释放一个UINavigationController?
我的应用程序设置是一个带有三个选项卡的 UITabBar。每个选项卡中都有一个不同的 UINavigationController。
在第一个选项卡中有一个刷新按钮 - 这会加载来自 Web (xml) 的数据。数据显示在三个选项卡栏中。
当有人刷新数据时,如何释放 UINavigationControllers?我想要这样做的原因是,当数据更改时,各个选项卡可能会显示全新的数据,因此保留该屏幕会有点危险..如果这是有意义的。因此,我想完全刷新 UINavigationControllers 并在再次单击选项卡时显示导航堆栈中的第一个视图。
感谢瑞安的回答。我做的方式是这样的
for(UINavigationController *navController in [self.navigationController.tabBarController viewControllers]) {
NSLog(@"popping %@", [navController title]);
[navController popToRootViewControllerAnimated:NO];
if ([[navController title] isEqualToString:@"Tab2"])
{
Tab2RootController *newRoot2 = [[Tab2RootController alloc] initWithNibName:@"Tab2RootController" bundle:nil];
newRoot2.title = @"Tab2";
[navController setViewControllers:[NSArray arrayWithObject:newRoot2] animated:NO];
//need [newRoot2 release]?
}
if ([[navController title] isEqualToString:@"Tab3"])
{
Tab3RootController *newRoot = [[Tab3RootController alloc] initWithNibName:@"Tab3RootController" bundle:nil];
newRoot3.title = @"Tab3";
[navController setViewControllers:[NSArray arrayWithObject:newRoot3] animated:NO];
//need [newRoot3 release]?
}
}
My app setup is a UITabBar with three tabs. In each tab is a different UINavigationController.
In the first tab there is a refresh button - this loads in a load of data from the web (xml). The data is displayed in the three tab bars.
How do I release the UINavigationControllers whenever someone refreshes the data? The reason I want to to this is when then data changes the various tabs might have completely new data to display, so it would be a bit dangerous to keep that screen .. if that makes sense. So I want to completely refresh the UINavigationControllers and show the first view in the navigation stack when they click on a tab again.
Thanks to Ryan for his answer. The way I did was something like this
for(UINavigationController *navController in [self.navigationController.tabBarController viewControllers]) {
NSLog(@"popping %@", [navController title]);
[navController popToRootViewControllerAnimated:NO];
if ([[navController title] isEqualToString:@"Tab2"])
{
Tab2RootController *newRoot2 = [[Tab2RootController alloc] initWithNibName:@"Tab2RootController" bundle:nil];
newRoot2.title = @"Tab2";
[navController setViewControllers:[NSArray arrayWithObject:newRoot2] animated:NO];
//need [newRoot2 release]?
}
if ([[navController title] isEqualToString:@"Tab3"])
{
Tab3RootController *newRoot = [[Tab3RootController alloc] initWithNibName:@"Tab3RootController" bundle:nil];
newRoot3.title = @"Tab3";
[navController setViewControllers:[NSArray arrayWithObject:newRoot3] animated:NO];
//need [newRoot3 release]?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不赞同你想做的事情,但这是如何做到的:
干杯!
Not endorsing what you are wanting to do, but this is how to do it:
Cheers!
popToRootViewController 将弹出堆栈上除根视图控制器之外的所有视图控制器。如果您还想摆脱根视图控制器,则需要使用 -[UINavigationController setViewControllers:animated:] 完全替换它。当然,您需要已经设置新的根视图控制器。它看起来像这样(修改上面 MattLeff 的答案):
请注意,这将立即切换视图控制器,没有动画。通过为每个导航栏设置视图控制器的完整列表,它们将释放旧的视图控制器,并且只要没有其他人保留它们,它们就会被释放。
popToRootViewController will pop all the view controllers on the stack except for the root view controller. If you want to also get rid of the root view controller, you need to replace it entirely, using -[UINavigationController setViewControllers:animated:]. Of course, you'll need to have your new root view controller set up already. It would look something like this (amending MattLeff's answer, above):
Note that this will instantly switch the view controllers, with no animations. By setting the entire list of view controllers for each navbar, they will release the old view controllers, and they'll be deallocated as long as no one else has retained them.