這次講的內容很簡單:
1.首先我們還是建立一個Single View Application,然後開啟MainStoryboard_iphone.storyboard,在裡面放入倆lable和兩個TextFiled:
2.接著開始寫代碼:ViewController.h:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{UITextField *nameField;UITextField *numberField;}@property (nonatomic,retain) IBOutlet UITextField *nameField;@property (nonatomic,retain) IBOutlet UITextField *numberField;- (IBAction)backgroundTap:(id)sender;- (IBAction)textFiledReturnEditing:(id)sender;@end
ViewController.m:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize nameField;@synthesize numberField;- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view.}//點擊螢幕空白view時觸發的事件- (IBAction)backgroundTap:(id)sender{ [nameField resignFirstResponder];//通知文本失去第一響應者狀態 [numberField resignFirstResponder];}//點擊return時觸發的事件ß- (IBAction)textFiledReturnEditing:(id)sender { [sender resignFirstResponder];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; }}@end
3.接著我們串連操作和輸出口:
將背景view的類別設定為UIControl,這樣我們就能對螢幕的事件進行處理了,將Control的touch down輸出串連到backgroundTap事件上,因為點擊軟鍵盤會觸發did end on exit,那我們就把兩個textFiled的did end on exit輸出串連到textFiledReturnEditing事件上。當然我們不要忘記將兩個textFiled控制項的輸出與ViewController的相應控制項介面串連在一起。
4.運行程式看看效果:
點擊textFiled時:
點擊return或點擊介面空白時: