0

我是新手,正在尝试找出一些简单的任务。

我有两个视图控制器。

第一个是登录屏幕。

它有两个字段和一个登录按钮。

如果用户输入的用户名和密码正确,我只想将他们引导到下一个视图。

现在,如果我单击登录按钮,它将转到第二个屏幕。

我想知道如何验证用户名和密码(可以硬编码),如果它不正确,那么不要进入下一个屏幕。如果正确,则转到下一个屏幕。

我知道这很简单,但由于某种原因我找不到任何东西。

头文件#import

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
- (IBAction)LoginButton:(id)sender;
- (IBAction)backgroundClick:(id)sender;

@end

主文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize userNameField, passwordField;
- (void)viewDidLoad
{

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];


    // Dispose of any resources that can be recreated.
}




- (IBAction)LoginButton:(id)sender {
}

- (IBAction)backgroundClick:(id)sender {
    [userNameField resignFirstResponder];
    [passwordField resignFirstResponder];
}
@end
4

2 回答 2

2

如果你想检查你的用户名和密码,那么你应该在你的方法中这样检查

- (IBAction)LoginButton:(id)sender {

    if([userNameField.text isEqualToString:@"username"] && [passwordField.text isEqualToString:@"pwd"]){

    NewViewController *newViewController=[[NewViewController alloc]initWithNibName:@"NewViewController" bundle:nil];
        [self presentViewController:newViewController animated:YES completion:nil];

    }
    else {

      NSLog(@"username or password is correct");

     }

}
于 2013-03-03T03:24:03.653 回答
1

在你的

- (IBAction)LoginButton:(id)sender {
}

方法,你需要实现这样的东西:

if([userNameField.text isEqualToString:@"userName"] && [passwordField.text isEqualToString:@"password"]){
    NSLog)@"successful login");
    //then load your new view
    YourViewController *yourVC=[[YourViewController alloc] initWithNibName:@"yourVC" bundle:[NSBundle mainBundle]];
    [self presentViewController:yourViewController animated:YES completion:nil];
}
else{
    NSLog(@"Invalid UserName Password");
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Login Failed"    
                           message:@"Invalid user name and/or password"
                          delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil, nil];
    [alertView show];
}
于 2013-03-03T03:23:14.567 回答