• 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;
    }
  • 相关阅读:
    Oracle11g工具
    Oracle数据库中scott用户的所有表结构
    Oracle数据库手动解锁scott用户
    Oracle数据库实例的删除和安装
    Oracle数据库的安装
    Oracle数据库发展历史
    禁止浏览器中双击选中元素的解决方法
    Window 设置pm2开机自启动服务
    通过node创建web服务器----express插件打包上线
    vue项目优化----通过externals加载外部CDN资源
  • 原文地址:https://www.cnblogs.com/cqchen/p/3764251.html
Copyright © 2020-2023  润新知