iOS_8_鍵盤的簡單處理

來源:互聯網
上載者:User

iOS_8_鍵盤的簡單處理

最終:




<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjwvcD4KPHAgY2xhc3M9"p1">BeyondViewController.h

////  BeyondViewController.h//  9_鍵盤的簡單處理////  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import @interface BeyondViewController : UIViewController- (IBAction)exitKeyboard:(UIButton *)sender;@end





BeyondViewController.m

////  BeyondViewController.m//  9_鍵盤的簡單處理/*    存在的問題:    1,彈出的鍵盤可能會遮住介面上的控制項,解決方案:使用scrollView,或,動態減少控制項的Y值(使之上移)    2,toolBar上面的兩個按鈕的點擊事件,還沒有實現 *///  Created by beyond on 14-7-25.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondViewController.h"@interface BeyondViewController (){    // 鍵盤上面的附屬工具條    UIToolbar *_toolBar;}@end@implementation BeyondViewController- (void)viewDidLoad{    [super viewDidLoad];        // 為所有鍵盤上面添加工具條    [self addToolBarForKeyboard];}// 為所有鍵盤上面添加工具條- (void)addToolBarForKeyboard{    // mainBundel載入xib,副檔名不用寫.xib    NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"KeyToolBar" owner:nil options:nil];    // 鍵盤附屬工具條    _toolBar = arrayXibObjects[0];    // 為self.view內部的所有文本輸入框設定toolBar    NSArray *array = self.view.subviews;    for (UIView *obj in array) {        if ([obj isKindOfClass:[UITextField class]]) {            // 為什麼要強轉,因為UIView的屬性 inputAccessoryView 是readOnly            UITextField *obj2 = (UITextField *)obj;            obj2.inputAccessoryView = _toolBar;        }    }        // 為toolBar中的第1個UIToolbarTextButton(上一個按鈕)添加點擊事件    //[[_toolBar.subviews firstObject] addTarget:self action:@selector(previousKeyboard:) forControlEvents:UIControlEventTouchUpInside];        // 為toolBar中的第2個UIToolbarTextButton(下一個按鈕)添加點擊事件    //[[_toolBar.subviews objectAtIndex:2] addTarget:self action:@selector(nextKeyboard:) forControlEvents:UIControlEventTouchUpInside];        // 為toolBar中的最後一個UIToolbarTextButton(完成按鈕)添加點擊事件    [[_toolBar.subviews lastObject] addTarget:self action:@selector(exitKeyboard:) forControlEvents:UIControlEventTouchUpInside];}// toolBar裡面,點擊上一個按鈕- (void)previousKeyboard:(UIButton *)sender{    NSLog(@"點擊了上一個按鈕,要啟用上一個輸入框");}// toolBar裡面,點擊下一個按鈕- (void)nextKeyboard:(UIButton *)sender{    NSLog(@"點擊了下一個按鈕,要啟用下一個輸入框");}// 退出鍵盤- (IBAction)exitKeyboard:(UIButton *)sender {    // 方式1: self.view內部所有的文字框(包括子孫控制項...)都退出第一響應者    [self.view endEditing:YES];    return;        // 方式2:    // 遍曆uiview裡面所有的控制項 ,resignFirstResponder    /*        for (int i=0; i



KeyToolBar.xib


















聯繫我們

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