zoukankan      html  css  js  c++  java
  • iOS.UIKit.03.UITextField_UITextView

    一、案例介绍:包含UITextField和UITextView,键盘可以打开、关闭,如图01。

    图01图02图03

    二、案例步骤:

    1、选择Single View Application新建项目,取名cq.28.TextField和TextView,如图02。

    2、Main.storyboard如图03。

    3、CQ28ViewController.h代码

    》实现UITextFieldDelegate、UITextViewDelegate,控制软键盘的关闭

    #import <UIKit/UIKit.h>
    
    @interface CQ28ViewController : UIViewController<UITextFieldDelegate,UITextViewDelegate>
    
    @end

    4、CQ28ViewController.m代码

    》键盘打开通知,键盘关闭通知

    -(void) viewWillAppear:(BOOL)animated {
        
        //注册键盘出现通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:)
                                                     name: UIKeyboardDidShowNotification object:nil];
        //注册键盘隐藏通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:)
                                                     name: UIKeyboardDidHideNotification object:nil];
        [super viewWillAppear:animated];
    }
    
    
    -(void) viewWillDisappear:(BOOL)animated {
        //解除键盘出现通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name: UIKeyboardDidShowNotification object:nil];
        //解除键盘隐藏通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name: UIKeyboardDidHideNotification object:nil];
        
        [super viewWillDisappear:animated];
    }
    
    -(void) keyboardDidShow: (NSNotification *)notif {
        NSLog(@"键盘打开");
    }
    
    -(void) keyboardDidHide: (NSNotification *)notif {
        NSLog(@"键盘关闭");
    }

    》实现委托放弃第一响应者

    //通过委托来实现放弃第一响应者
    #pragma mark - UITextField Delegate Method
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    
    
    //通过委托来实现放弃第一响应者
    #pragma mark - UITextView Delegate  Method
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
    {
        if([text isEqualToString:@"
    "]) {
            [textView resignFirstResponder];
            return NO;
        }
        return YES;
    }
  • 相关阅读:
    hdu 4324(dfs)
    hdu 2376(求树上任意两点之间距离之和的平均值)
    hdu 3665(最短路)
    hdu 4463(最小生成树变形)
    hdu 2242(边双连通分量)
    hdu 2682(最小生成树)
    hdu 2444(二分图的判断以及求最大匹配)
    陶哲轩实分析命题6.4.12
    陶哲轩实分析习题8.3.4
    CantorBernsteinSchroeder定理的证明
  • 原文地址:https://www.cnblogs.com/cqchen/p/3764251.html
Copyright © 2011-2022 走看看