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


















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

  • 相关阅读:
    springboot springcloud zuul 过滤器
    springboot springcloud eureka 熔断器
    javaweb servlet filter
    maven nexus 搭建私服(二)
    springboot springcloud zuul 网关入门
    springboot springcloud 配置中心
    springboot springcloud eureka 入门
    java rabbitmq
    java jvm调优
    maven nexus 搭建私服(一)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4876471.html
Copyright © 2011-2022 走看看