Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

kirkbyo
kirkbyo
15,791 Points

Change View Controllers without using a navigation controller

I have a login and sign up page on my app currently using the code we used in the Ribbit app. I have tried using the method in this video but that method does not work in my case because I am using an if/else statement.

I want to have it so that when the user logs in and everything is correct to change the view controller to the home view controller for example.

Current Code

- (IBAction)login:(id)sender {
    NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if([username length] == 0 || [password length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Make sure you enter a username and password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];
    }else{
        [PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
            if (error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alertView show];
                NSLog(@"Login failed!");
            }else {
                [self.navigationController popToRootViewControllerAnimated:YES];
                NSLog(@"Login was successful");
            }
        }];
    }
}

Any help is appreciated,

Ozzie

3 Answers

kirkbyo
kirkbyo
15,791 Points

I found the solution for anyone in the future that has the same problem.

NSString * storyboardName = @"NameOfYourStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"nameOfViewController"];
[self presentViewController:vc animated:YES completion:nil];

After a bit of googling I found the answer on stack overflow. I hope this saves someone the headache :)

Ozzie

In the the second else statement you have : [self.navigationController popToRootViewControllerAnimated:YES]; which will return the user to the root view controller (the first one/home) using the navigation controller, so if you want to perform a segue instead of returning to the root view controller you should remove that line.

P.S the video that you have linked to is really bad example, if you want to perform segue programmatically you should click on the view controller then ctrl + click on the yellow circle bellow it and link it to another view controller , and then you "performSegueWithIdentifier:sender" method where you want (in your case the second else statement)

kirkbyo
kirkbyo
15,791 Points

Thank you for your answer Mohammad, but I am a bit confused in what your are saying. Should I follow the video but do what you suggested? or just forget the video all together and find another way?

find a new tutorial, and remember to remove [self.navigationController popToRootViewControllerAnimated:YES]; do you don't use the navigation controller

kirkbyo
kirkbyo
15,791 Points

Ok thank you for your time