ios學習筆記(四)收回軟鍵盤的兩種方式
這次講的內容很簡單:
1.首先我們還是創建一個Single View Application,然后打開MainStoryboard_iphone.storyboard,在里面放入倆lable和兩個TextFiled:
2.接著開始寫代碼:ViewController.h:
#import@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或點擊界面空白時:
轉自:http://blog.csdn.net/itachi85/article/details/7664563
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!