Reputation: 470
I read this tutorial about storyboards on iphone dev. The guy changes views through a button. But the connection of the button is made through the interface builder. How can i do this programmatically, so i can make some checks (username/password for example) before the view is changed? Thanks in advance.
Upvotes: 1
Views: 831
Reputation: 38728
You can either create the button and drag the touchUpInside action to an IBAction
in interface builder
or in code somewhere in viewDidLoad
[self.login addTarget:self action:@selector(loginTapped) forControlEvents:UIControlEventTouchUpInside];
Then the loginTapped
method could look something like
- (void)loginTapped;
{
if ([self authenticateWithUsername:self.username.text password:self.password.text]) {
[self performSegueWithIdentifier:@"loginSuccessful" sender:self];
} else {
// Warn user about invalid username/password
}
}
This relies on you having created the segue in the storyboard with a name that matches the identifier
argument
Upvotes: 0
Reputation: 623
Connect one view with other (not the button with the view). Change the segue to custom and give it an identifier (in IB). Ex: Login
Then create an action and assign it to the button.
In the button action use:
[self performSegueWithIdentifier:@"Login" sender:self];
Create a Segue Class object and in the .m use something like:
UIViewController *dst = [self destinationViewController];
UIViewController *nav = [self sourceViewController];
//All your custom code
[nav presentModalViewController:dst animated:YES];
Edit: Forgot something important!
In IB, in the Segue configuratión, put in Segue Class, the name of the Segue Class created. Ex: LoginSegue is the name of the segue created, in Segue Class yo has to write "LoginSegue"
Edit: To Create the Class:
1.- Create a New File extending UIStoryBoardSegue, the .h gonna be something like:
#import <Foundation/Foundation.h>
@interface LoginSegue : UIStoryboardSegue
@end
2.- In the implementation, use the code above inside an method called perform:
-(void)perform
{
UIViewController *dst = [self destinationViewController];
UIViewController *nav = [self sourceViewController];
//Custom Code
[nav presentModalViewController:dst animated:YES];
}
If you need to access the property of the source viewController, you need to change:
UIViewController *nav = [self sourceViewController];
to
eYourClass *nav = [self sourceViewController];
Hope this help!
Upvotes: 2