zoukankan      html  css  js  c++  java
  • iOS_8_键盘操作简单

    最后效果图:





    BeyondViewController.h

    //
    //  BeyondViewController.h
    //  9_键盘的简单处理
    //
    //  Created by beyond on 14-7-25.
    //  Copyright (c) 2014年 com.beyond. All rights reserved.
    //
    
    #import <span style="font-family: Arial, Helvetica, sans-serif;"><UIKit/UIKit.h></span>
    
    @interface BeyondViewController : UIViewController
    - (IBAction)exitKeyboard:(UIButton *)sender;
    
    @end
    
    </uikit>





    BeyondViewController.m

    //
    //  BeyondViewController.m
    //  9_键盘的简单处理
    /*
        存在的问题:
        1,弹出的键盘可能会遮住界面上的控件,解决方法:使用scrollView,或,动态降低控件的Y值(使之上移)
        2,toolBar上面的两个button的点击事件,还没有实现
     */
    //  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(上一个button)加入点击事件
        //[[_toolBar.subviews firstObject] addTarget:self action:@selector(previousKeyboard:) forControlEvents:UIControlEventTouchUpInside];
        
        // 为toolBar中的第2个UIToolbarTextButton(下一个button)加入点击事件
        //[[_toolBar.subviews objectAtIndex:2] addTarget:self action:@selector(nextKeyboard:) forControlEvents:UIControlEventTouchUpInside];
        
        // 为toolBar中的最后一个UIToolbarTextButton(完毕button)加入点击事件
        [[_toolBar.subviews lastObject] addTarget:self action:@selector(exitKeyboard:) forControlEvents:UIControlEventTouchUpInside];
    }
    // toolBar里面,点击上一个button
    - (void)previousKeyboard:(UIButton *)sender
    {
        NSLog(@"点击了上一个button,要激活上一个输入框");
    }
    // toolBar里面,点击下一个button
    - (void)nextKeyboard:(UIButton *)sender
    {
        NSLog(@"点击了下一个button,要激活下一个输入框");
    }
    
    // 退出键盘
    - (IBAction)exitKeyboard:(UIButton *)sender {
        // 方式1: self.view内部全部的文本框(包含子孙控件...)都退出第一响应者
        [self.view endEditing:YES];
        return;
        
        // 方式2:
        // 遍历uiview里面全部的控件 ,resignFirstResponder
        /*
            for (int i=0; i<self.view.subviews.count; i++) {
                [self.view.subviews[i] resignFirstResponder];
            }
         */
        NSArray *array = self.view.subviews;
        for (UIView *obj in array) {
            if ([obj isKindOfClass:[UITextField class]]) {
                [obj resignFirstResponder];
            }
        }
        
        // 方式3:
        // 在self.view的最开头,铺一个全屏的透明的button,连线,仅仅要在屏幕空白区域点击后,就能够调用上面的方式1,退出键盘
        
        
    }
    
    @end
    
    




    KeyToolBar.xib


















    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    第三章 对话拖延--摆脱拖延
    第二章 审问拖延:被架在审判席上的失败者恐惧症
    番茄学习法笔记以及初始尝试
    第一章 质疑拖延:是可恶的坏习惯还是应得的报应
    学习编程珠玑笔记记录-----第二章 算法
    李文业工作一年总结
    永远要努力向前
    怎样才能在工作中进步的更快
    windows下php扩展安装
    JS学习笔记
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4876471.html
Copyright © 2011-2022 走看看