Present and dismiss modal view controller

前端 未结 6 927
广开言路
广开言路 2020-11-28 04:45

Can anyone give me the example code that I can use to first present a modal view controller, then dismiss it? This is what I have been trying:

NSLog(@\"%@\",         


        
6条回答
  •  忘掉有多难
    2020-11-28 05:15

    The easiest way to do it is using Storyboard and a Segue.

    Just create a Segue from the FirstViewController (not the Navigation Controller) of your TabBarController to a LoginViewController with the login UI and name it "showLogin".

    Create a method that returns a BOOL to validate if the user logged in and/or his/her session is valid... preferably on the AppDelegate. Call it isSessionValid.

    On your FirstViewController.m override the method viewDidAppear as follows:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        if([self isSessionValid]==NO){
            [self performSegueWithIdentifier:@"showLogin" sender:self];
        }
    }
    

    Then if the user logged in successfully, just dismiss or pop-out the LoginViewController to show your tabs.

    Works 100%!

    Hope it helps!

提交回复
热议问题