Custom segue after successful login

2019-09-02 18:56发布

问题:

I'm a beginner in iOS programming, so please don't kill me :]. I have application with authorization and when I successful authorize I would like go to second view. When authorization fails I show alert. My code for the authorization:

- (IBAction)LoginClick:(id)sender {

    for(User *user in self.allUsers){
        if([user.userName isEqualToString:self.usernameTextField.text] && [user.password isEqualToString:self.passwordTextField.text]){
            // Logged in --> go to second view (TableView)
            [self performSegueWithIdentifier:@"LoginSegue" sender:self];
        }
        else{
            // Wrong password or username
            Alerts *alert = [Alerts new];
            [alert showAlert:LOGIN_ERROR];
        }
    }
}

I tried to look a simple way for custom transition (segue) to second view, when I successful log in, but I didn't find. Now I have this storyboard:

https://www.dropbox.com/s/dc8h4u920wgt47s/Screenshot%202014-03-03%2011.36.56.png

And code for transition between View is:

[self performSegueWithIdentifier:@"LoginSegue" sender:self];

The result is error:

* Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'LoginSegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'

What is the problem? Why I need something like "Navigation controller". I need to create a class for segue? What is the simplest way to solve this problem? Thank you.

EDIT ------- SOLUTION

Thanks to all for help. I added navigation controller to my application as initial view. And I found, that use of a custom segue and making class for this custom segue is unnecessary. The simplest way for solution my problem is connect the ViewControllers (NOT BUTTON and second ViewController, so I had it first time...) and name the push segue. The name I used here in code:

- (IBAction)LoginClick:(id)sender {

    for(User *user in self.allUsers){
        if([user.userName isEqualToString:self.usernameTextField.text] && [user.password isEqualToString:self.passwordTextField.text]){
            // Logged in --> go to second view (TableView)
            [self performSegueWithIdentifier:@"LoginSegue" sender:self];
        }
        else{
            // Wrong password or username
            Alerts *alert = [Alerts new];
            [alert showAlert:LOGIN_ERROR];
        }
    }
}

Concrete here: [self performSegueWithIdentifier:@"LoginSegue" sender:self];

And all works great. When I login with correctly pass and username I'm redirected to second View.

回答1:

The error is telling you you can't use that type of transition without using a navigation controller.

Simply drag and drop a navigation controller from the right menu and set it as the Initial Controller in the properties section on the right. like so:

Then connect your login controller as its root view controller. Then this will work for you.

The end result should look something like:



回答2:

You should use Modal segue when you do not use UINavigationController instead of Push segue.



回答3:

There could do be two things you could do, which in all likelihood you're not doing. The Navigation Controller might be missing and it needs to be the root view controller. Adding navigation controller to the project is actually very easy. Open the story board and select the first view, then go to.

Editor>Embed In> Navigation Controller

If that doesn't work you can instantiate an object of the next view controller and do an explicit segue in the prepare for segue method.

NextViewController *nvc = [segue destinationViewController];

To understand what the navigation controller does follow this link



回答4:

In order to utilize a push segue, your root view controller needs to be a navigation controller. The only segue you can use without a "master" controller of sorts like that, is a modal segue



回答5:

when you use segues,your firstViewController should be under Navigation controller. If you have not already embedded vc in Navigation controller then click on VC in storyboard and on top of xcode click in

editor -> embed -> navigationController