iOS简单认证



我是新来的,想弄清楚一些简单的任务。

有两个视图控制器

第一个是Login屏幕。

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

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

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

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

我知道这很简单,但是我就是找不到任何东西。

头文件#进口

@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

如果你想检查你的用户名&那么你应该在方法

中这样检查
- (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");
     }
}

- (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];
}

最新更新