IOS開發學習筆記六 實現一個簡單的登陸介面
阿新 • • 發佈:2019-01-31
首先是要實現的效果圖:demo下載
首先在介面拖拽兩個Label,分別命名為姓名和密碼;
新增一個txtAccount,Text控制元件的placeholder類似於Android的Hint一樣,這裡設定為“請輸入賬號”,clearButton設定之後右側會出現一個X號來進行輸入資訊的清楚,keyboard Type的型別設定為number pad,這樣控制元件只允許輸入數字:
再新增一個txtPsd,設定placeholder為“請輸入密碼”,然後設定clearButton為is always visible,設定keyboard Type的型別設定為ASCII Capable,這樣控制元件允許輸入字母和數字:
最後我們再分別為txtAccount和txtPsd新增對應的物件,併為登陸按鈕新增btnLogin點選事件,具體程式碼如下:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *txtAccount;
@property (weak, nonatomic) IBOutlet UITextField *txtPsd;
- (IBAction)btnLogin;
@end
@implementation ViewController
- (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)btnLogin {
NSString * account = self .txtAccount.text;
NSString * psd = self.txtPsd.text;
NSLog(@"QQ賬號:%@;QQ密碼:%@",account,psd);
if([account isEqual:@"123456"] && [psd isEqual:@"123456"]){
NSLog(@"登陸成功");
}else{
NSLog(@"登陸失敗");
}
[self.view endEditing:YES];
}
@end