zoukankan      html  css  js  c++  java
  • iOS TextView内容为空时设置按钮为不可用

    有时做项目会遇到这样一种需求: 当一个TextView内容为空时设置按钮为不可用, 具体实现如下:

    1. 按钮初始设置为不可用:

    self.customButton.enabled = NO;

    2. 通过代理或通知监控TextView文本内容变化状态, 通过hasText方法判断TextView内是否有文本内容, 以设置按钮状态:

    代理:

    遵守协议:

    @interface CustomViewController () <UITextViewDelegate>

    设置代理:

    self.textView.delegate = self;

    实现代理方法:

    - (void)textViewDidChange:(UITextView *)textView
    {
        self.customButton.enabled = textView.hasText;
    }

    通知:

    设置通知监听者及对象:

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged) name:UITextViewTextDidChangeNotification object:self.textView];

    实现通知方法:

    - (void)textDidChanged
    {
        self.customButton.enabled = self.textView.hasText;
    }

    移除通知监听者:

    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
  • 相关阅读:
    如何掌握所有的程序语言
    程序与证明
    卫星通信地面系统构成
    SCIP 环境搭建
    Homebrew install.sh
    macOS 内核之从 I/O Kit 电量管理开始
    matlab练习程序(空间椭圆拟合)
    多进程抢票加锁
    进程间数据传递
    队列用法
  • 原文地址:https://www.cnblogs.com/happyplane/p/4728774.html
Copyright © 2011-2022 走看看