I am new to objective-c and have recently started making iOS applications in Xcode. I was making a login application that has two view controllers. It looks like this: http://imgur.com/W3nxMEG
Now my issue is that I was the app to go to second ViewController only if: 1) Text fields are filled 2) Passwords match
that's the code that I've written. I couldnt find any command that controls the transition between the viewControlled based on the certain constraints:
(IBAction)loginButton:(id)sender {
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:@""];
emptyPass = [password isEqualToString:@""];
emptyrePass = [reEnter isEqualToString:@""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"You must complete all the fields " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
else{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Passwords match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Passwords dont match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
}
Shall be thankful if anyone can help me solve this
First make sure your UITextField
and UIButton
from your storyBoard are hooked with a property in LoginViewController.h
It'll look like this:
LoginViewController.h
@interface LoginViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *password;
@property (strong, nonatomic) IBOutlet UITextField *username;
// and so on..
// for your button
@property (strong, nonatomic) IBOutlet UIButton *loginButton;
@end
LoginViewController.m
@implementation LoginViewController
-(IBAction)loginButton:(id)sender
{
// you can enable/disable your button here also, by:
// UIButton *senderButton = (UIButton *)sender;
// senderButton.enabled = YES/NO;
// i'm just using .length because i trust numbers more that the @""
NSString *errorMessage;
if (self.username.text.length == 0)
errorMessage = @"username is required";
else if (self.password.text.length == 0)
errorMessage = @"password is required";
else if (self.reEnter.text.length == 0)
errorMessage = @"please confirm your password";
else if (self.password.text.length == 0 || self.username.text.length == 0 || self.reEnter.text.length == 0)
errorMessage = @"You must complete all the fields";
if (errorMessage.length > 0)
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
else
{
if ([self.username.text isEqual: @"youUsername"] && [self.password.text isEqual: @"youPassword"] && [self.password.text isEqual: self.reEnter.text])
{
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Login successful" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[pass show];
//presentYourSecondViewController
//[self presentViewController:(UIViewController) animated:(BOOL) completion:nil];
//[self.navigationController pushViewController:(UIViewController)animated:(BOOL)];
}
else
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Login failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
}
/* Your code...
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:@""];
emptyPass = [password isEqualToString:@""];
emptyrePass = [reEnter isEqualToString:@""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"You must complete all the fields " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
else
{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:@"Success!" message:@"Passwords match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Passwords dont match " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
}
*/
}
@end
There is nothing wrong with your condition i just over did it i guess.. Anyway hope i've help you Happy coding cheers..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.