ios學習筆記(四)收回軟鍵盤的兩種方式

來源:互聯網
上載者:User

這次講的內容很簡單:

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或點擊介面空白時:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.