简体   繁体   English

如何将视图控制器推送到iOS中标签栏中的导航控制器

[英]how to push a view controller to a navigation controller in a tab bar in iOS

I have a tab bar controller with a navigation controller per tab. 我有一个标签栏控制器,每个标签有一个导航控制器。 The nav controllers have view controllers. 导航控制器具有视图控制器。 I then have a Login View Controller not connected to the tab bar controller that I call with the code below if a user needs to login or logs out. 然后,如果用户需要登录或注销,我的登录控制器没有连接到我用以下代码调用的标签栏控制器。

I am seeing some strange behavior when I try to conditionally push a view controller based on if a user is logged in or not. 当我尝试根据用户是否登录时有条件地推送视图控制器时,我看到一些奇怪的行为。

My logic looks like this: 我的逻辑看起来像这样:

if(currentUser){
}else{

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

    }

When the view is pushed it look as if the the Login View is pushed and another Login View is pushed on top of it. 按下视图时,看起来好像已按下了“登录视图”,并且在其上方推送了另一个“登录视图”。

For logout I have the same code in a segue: 对于注销,我在segue中有相同的代码:

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

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

In this instance I see the same double push and each time I click my login button it pushes another Login View. 在这个例子中,我看到相同的双推,每次我点击我的登录按钮,它推动另一个登录视图。 This happens infinitely. 这种情况无限发生。 I then get the warning: 然后我收到警告:

Finishing up a navigation transition in an unexpected state. 在意外状态下完成导航过渡。 Navigation Bar subview tree might get corrupted. 导航栏子视图树可能已损坏。

When I press back in the navigation, the app crashes with the error: 当我在导航中按回来时,应用程序崩溃并显示错误:

NSInvalidArgumentException', reason: 'Can't add self as subview' NSInvalidArgumentException',原因:'无法将自己添加为子视图'

Am I not correctly pushing the login view controller? 我没有正确推送登录视图控制器吗?

Segues instantiate new controllers, and perform the transition form the source controller to the destination controller. Segues实例化新控制器,并执行从源控制器到目标控制器的转换。 Therefore, you should neither be instantiating the controller in code, nor pushing it with pushViewController:animated:. 因此,您既不应该在代码中实例化控制器,也不应该使用pushViewController推送它:animated:。 If the buttons (rather than the controller) are triggering the segues, then you only need to get a reference to the destination controller (segue.destinationViewController), and use that to hide the bottom bar, 如果按钮(而不是控制器)正在触发segue,那么您只需要获取对目标控制器的引用(segue.destinationViewController),并使用它来隐藏底栏,

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

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

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

If you need to do this conditionally, then the segue should be connected directly from the controller, not a button. 如果您需要有条件地执行此操作,则segue应直接从控制器连接,而不是按钮。 Then you need to call performSegueWithIdentifier: in some method where you have the logic to determine which (or whether a) segue should be performed. 然后你需要调用performSegueWithIdentifier:在某种方法中你有逻辑来确定应该执行哪个(或者是否)segue。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从AppDelegate推送到View Controller而不会丢失导航栏和标签栏设置iOS - Can't push to View Controller from AppDelegate without losing Navigation Bar and Tab Bar setup iOS 在标签栏中推送View Controller - Push view Controller in the tab bar iOS-导航控制器到选项卡栏控制器 - iOS - navigation controller to tab bar controller IOS-标签栏控制器和导航控制器 - IOS - Tab Bar Controller and Navigation Controller iOS Push Navigation Controller,第二个视图上没有栏 - iOS Push Navigation Controller, without a bar on the second view iOS状态栏-View Controller到Navigation Controller - iOS status bar - View Controller to Navigation Controller 如何在导航栏、标签栏、视图控制器之间传递数据 - How to pass data among Navigation bar, Tab bar, View Controller 如何使用标签栏项添加导航栏以查看控制器 - How to add Navigation bar to view controller with tab bar item 从导航栏视图控制器导航到选项卡栏视图控制器 - Navigate from a navigation bar view controller to a tab bar view controller 单击推送通知时,正确重定向到选项卡栏导航控制器内的视图控制器 - Properly redirect to a view controller inside tab bar's navigation controller when push notification is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM