ios - 如何将 View Controller 推送到 iOS 标签栏中的导航 Controller

标签 ios cocoa-touch cocoa uinavigationcontroller uistoryboard

我有一个标签栏 Controller ,每个标签都有一个导航 Controller 。导航 Controller 有 View Controller 。然后我有一个登录 View Controller 未连接到选项卡栏 Controller ,如果用户需要登录或注销,我会使用下面的代码调用它。

当我尝试根据用户是否登录有条件地推送 View Controller 时,我看到了一些奇怪的行为。

我的逻辑是这样的:

if(currentUser){
}else{

        LoginViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
        svc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:svc animated:YES];

    }

当 View 被推送时,看起来好像登录 View 被推送,另一个登录 View 被推送到它上面。

对于注销,我在 segue 中有相同的代码:

if ([segue.identifier isEqualToString:@"LogoutView"]) {

        [self logOut];
        LoginViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
        svc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:svc animated:YES];
    }

在这种情况下,我看到了相同的双推,每次我单击我的登录按钮时,它都会推送另一个登录 View 。这种情况会无限发生。然后我收到警告:

Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

当我在导航中按返回键时,应用程序崩溃并出现错误:

NSInvalidArgumentException', reason: 'Can't add self as subview'

我是否没有正确推送登录 View Controller ?

最佳答案

Segues 实例化新 Controller ,并执行从源 Controller 到目标 Controller 的转换。因此,您既不应该在代码中实例化 Controller ,也不应该使用 pushViewController:animated: 推送它。如果按钮(而不是 Controller )触发了 segue,那么您只需要获取对目标 Controller (segue.destinationViewController) 的引用,并使用它来隐藏底部栏,

-(void)prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender {

    if ([segue.identifier isEqualToString:@"LogoutView"]) {

            [self logOut];
            LoginViewController *svc = segue.destinationViewController;
            svc.hidesBottomBarWhenPushed = YES;
    }
}

如果你需要有条件地这样做,那么 segue 应该直接从 Controller 连接,而不是按钮。然后你需要调用 performSegueWithIdentifier: 在一些你有逻辑来确定应该执行哪个(或是否)segue 的方法中。

关于ios - 如何将 View Controller 推送到 iOS 标签栏中的导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21638925/

相关文章:

ios - iOS框架中的重复符号

objective-c - 从 nsstring 中分离日期和时间的最佳方法是什么

objective-c - 如何向字符串添加度数符号?

ios - 警告 : Attempt to present ModalTableViewController on MainTableViewController which is already presenting (null)

ios - 如何检查 iOS 设备上的互联网连接?

java - org.eclipse.swt.cocoa.macosx.x.<version_no>.jar 与 org.eclipse.swt.<version_no>.jar 之间的区别?

objective-c - NSManagedObject 作为存储并持续分析原始数据

ios - XLPagerTabStrip 标签没有获得相等的屏幕宽度 :Swift

ios - 如何返回此函数的结果?

ios - 如何将数据写入项目目录中的文件?