我是objective-c的新手,最近开始在Xcode中开发iOS应用程序。我正在制作一个有两个视图控制器的登录应用程序。它看起来像这样:http://imgur.com/W3nxMEG
现在我的问题是我只是在以下情况下才能进入第二个ViewController: 1)填写文本字段 2)密码匹配
这是我写的代码。我无法找到任何控制viewControlled基于某些约束的转换的命令:
(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];
}
}
如果有人能帮我解决这个问题,请感恩。
答案 0 :(得分:0)
首先确保您的storyBoard中的UITextField
和UIButton
与LoginViewController.h
它看起来像这样:
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
你的情况没有任何问题我刚刚完成它我猜...无论如何希望我帮助你快乐编码欢呼..