目的:輸入帳號及密碼後,正確轉到另一頁面,錯誤顯示訊息。
Step 1.畫面上拉出"帳號"及“密碼”的Label & TextField,再來加入“登入”按鈕
如下圖
Step 2.設定帳號的TextField,如下圖
(1)Placeholder:浮水印字
(2)勾選"Clear when editing begins":代表要輸入時,先清除內容文字
(3)Keyboard:選擇"E-mail Address":讓要輸入帳號時,有email @文字可供選擇
Step 3.設定密碼的TextField,如下圖
(1)勾選Secure:此為密碼欄位
Step 4.ViewController.h,撰寫內容如下
@interface ViewController : UIViewController
{
IBOutlet UITextField *account;
IBOutlet UITextField *passwd;
}
@property (nonatomic, retain) IBOutlet UITextField *account;
@property (nonatomic, retain) IBOutlet UITextField *passwd;
-(IBAction)login:(id)sender;
Step 5.畫面上拖拉關聯如下
Step 6.新增“UIViewController subclass”命名為ContentView.
Step 7.ViewController.m,撰寫內容如下
@synthesize account, passwd;
-(IBAction)login:(id)sender {
NSString *usr = [account text];
NSString *pad = [passwd text];
iif([usr isEqualToString:@"john"] && [pad isEqualToString:@"123"]){
//設定動畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:2.0f];
NSArray *xibs = [[NSBundle mainBundle]loadNibNamed:@"ContentVies" owner:self options:NULL];
for(id obj in xibs){
if([obj isKindOfClass:[UIViewController class]]){
[self.navigationController pushViewController:obj animated:YES];
break;
}
}
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤訊息" message:@"登入失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
[alert show];
}
}



