Storyboard and views transitions

273 Views Asked by At

I'm currently learning iPhone applications development, and I made some online tutorials to learn how all this is working.

I'm now quite used to the Objective-C concepts, and I'm trying to build a first application based on two views :

The first view would be the "Login view", simply with a kind of login system : a login field and a password field, and a "connect" button.

The second view is the "Home view" of the application, which will be called after the login.

I made a push segue to make the relation between the Login view and the view that is called after login. Here's what the storyboard looks like :

Login system storyboard

What I don't know actually is how to call a function that will check if the credentials are correct, and the switch to the other view if the login succeed.

Can anyone explain me, or give me some tips / tutorials for this please ?

Here are the sources for my LoginController :

LoginController.h

@interface LoginController : UIViewController {
    IBOutlet UITextField *TFLogin;
    IBOutlet UITextField *TFPassword;
}

@property (strong, nonatomic) IBOutlet UITextField *TFLogin;
@property (strong, nonatomic) IBOutlet UITextField *TFPassword;

- (IBAction)Connect:(UIButton *)sender;


@end

LoginController.m

@implementation LoginController

@synthesize TFLogin;
@synthesize TFPassword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)Connect:(UIButton *)sender
{
    if ([TFLogin.text isEqualToString:@"myLogin"] && [TFPassword.text isEqualToString:@"myPassword"]) {
        [self performSegueWithIdentifier:@"LoginSegue" sender:sender];
        NSLog(@"Connection OK");
    }
    else {
        NSLog(@"Connection Not OK");
    }
}

@end

Thanks !

1

There are 1 best solutions below

5
samson On BEST ANSWER

You have two choices for triggering a segue. The easy way is just to ctrl-drag in interface builder from the button to the next view controller. You can also do it in code (in an IBAction), by calling performSegueWithIdentifier:sender:.

If you go with the IBAction, you can validate the data there.

If you go with the interface builder method, you can't validate -- prepareForSegue:sender: will be too late. Anyway, there's a possible stumbling block here -- as I recall, UINavigationController doesn't forward prepareForSegue:sender: to its children. You can mitigate this with a category on UINavigationController or by subclassing.