0

私は新しく、いくつかの簡単なタスクを理解しようとしています。

私は2つのビューコントローラーを持っています。

1つ目はログイン画面です。

2つのフィールドとログインボタンがあります。

ユーザーがユーザー名とパスワードを正しく入力した場合は、次のビューに移動するだけです。

今、ログインボタンをクリックすると、2番目の画面に移動します。

ユーザー名とパスワード(ハー​​ドコーディング可能)をどのように認証できるのか疑問に思っていました。正しくない場合は、次の画面に進まないでください。正しい場合は、次の画面に進みます。

簡単なことは知っていますが、どういうわけか何も見つかりません。

ヘッダーファイル#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 に答える