[英]popToRootViewController crashes when called by UITabBarControllerDelegate
我有一個帶有4個UINavigationControllers
的UITabBarController
。 我已經實現了didSelectViewController
委托方法,如下所示:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if ([viewController isKindOfClass:[UINavigationController class]]) {
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
在didSelectRowAtIndexPath
將新的viewController推入堆棧后,當NavigationController
處於第二級時,它會崩潰。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
RootViewController *detailViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
// ...
detailViewController.title = [self.temp objectAtIndex:indexPath.row];
detailViewController.sort = self.title;
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
當然啟用NSZombies
的調試器不會給出任何反饋。
但是,如果我向detailViewController alloc添加retain;
RootViewController *detailViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] retain];
它工作,但泄漏記憶。
任何想法有什么不對,如何解決,發生了什么?
我有類似的情況,我想出了以下解決方案。
在我的應用程序中,我在啟動時有登錄屏幕,然后我有UITabbarController與4 UINavigationControllers。
我在AppDelegate.h文件中創建了UINavigationController的屬性。
@property (strong, nonatomic) UINavigationController *navigationController;
然后
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
{
//Override point for customization after application launch.
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
現在,當您需要彈出RootViewController時,請使用以下代碼
#import "AppDelegate.h"
[((AppDelegate *)[[UIApplication sharedApplication] delegate]).navigationController popToRootViewControllerAnimated:YES];
希望這能解決你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.