首页 > 移动平台 > 详细

iOS TextView内容为空时设置按钮为不可用

时间:2015-08-16 13:37:48      阅读:262      评论:0      收藏:0      [点我收藏+]

有时做项目会遇到这样一种需求: 当一个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];
}

iOS TextView内容为空时设置按钮为不可用

原文:http://www.cnblogs.com/happyplane/p/4728774.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!