文本视图为空时禁用此按钮,否则启用 [英] the button is disabled when text view is empty else is enabled

查看:46
本文介绍了文本视图为空时禁用此按钮,否则启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发笔记应用程序,当文本视图为空时,应禁用完成按钮,以使用户无法将空笔记保存到数据库中,否则应启用该按钮.

I'm developing note app, when the text view is empty the done button should be disabled so user could not be able to save empty notes into data base, else the button should be enabled.

下面是我的代码,我的尝试失败了;我该如何解决这个问题?

here's my code below, my attempts have failed; how I can solve this problem?

 @IBOutlet weak var textView: UITextView!
 @IBOutlet weak var done: UIBarButtonItem!

 override func viewDidLoad() {
        super.viewDidLoad()

        title = note?.text
        if (self.textView.text.isEmpty){
            done.enabled = false
        }
        if let noteContent = note

        {
            textView.text = noteContent.text
        }

         self.navigationController!.toolbarHidden = false;
    }

  func textViewShouldBeginEditing(textView: UITextView) -> Bool{
done.enabled = true

    return done.enabled
}

推荐答案

  1. 使您的视图控制器符合UITextViewDelegate协议
  2. 在Interface Builder中,将文本视图上的delegate连接到视图控制器.
  3. 在您的视图控制器中添加以下功能:
  1. Make your view controller conform to UITextViewDelegate protocol
  2. In Interface Builder, connect the delegate on the text view to your view controller.
  3. Add the following function to your view controller:


func textViewDidChange(textView: UITextView) {
    if textView == self.textView {
        self.doneButton.enabled = !textView.text.isEmpty
    }
}

这篇关于文本视图为空时禁用此按钮,否则启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆