[iOS]UITableViewController完成收回鍵盤操作

來源:互聯網
上載者:User

標籤:uitableviewcontrolle

UITableViewController 本身能夠實現鍵盤適配(cell中控制項焦點會移動到鍵盤上方

在做鍵盤收回的時候思考過如下方案

1、tableview添加點擊事件

結果:點擊事件和tableview的didselect 衝突,導致didselect失效

2、scrollview代理滾動收回鍵盤

結果:目的可以達到,但是當點擊textfield的時候,此時鍵盤會出現之後直接收回。原因是先適配→調用scrollview代理。

最後採用如下方案



    <span style="font-family: Arial, Helvetica, sans-serif;">_sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];</span>    _sureBtn.frame = CGRectMake(self.view.size.width-60, 5, 50, 28);    _sureBtn.backgroundColor = [UIColor colorWithRed:0.150 green:0.662 blue:0.915 alpha:1.000];    _sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];    _sureBtn.layer.cornerRadius = 5.0;    [_sureBtn setTitle:@"確定" forState:UIControlStateNormal];    [_sureBtn addTarget:self action:@selector(changeRemarks) forControlEvents:UIControlEventTouchUpInside];    _view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];    _view.backgroundColor=[UIColor whiteColor];    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.5)];    line.backgroundColor = [UIColor blackColor];    [_view addSubview:line];    [_view addSubview:_sureBtn];


在使用的時候,對Textfield進如下處理

 _begoodatField.inputAccessoryView = _view;

如果是tableview中cell的文字框

則需要在定義一個臨時textfield。

使用的時候 

 _xuexiaoField=cellMenu.celltext; _xuexiaoField.inputAccessoryView = _view;//<span style="font-family: Arial, Helvetica, sans-serif;">cellMenu為自訂cell的名字</span>
附上確定按鈕方法
- (void)changeRemarks{    [_nicktextField resignFirstResponder];    [_xuexiaoField resignFirstResponder];    [_begoodatField resignFirstResponder ];    [_bumenField resignFirstResponder ];}



https://github.com/KKKKaras/SureBtnDemo


[iOS]UITableViewController完成收回鍵盤操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.