如何在JSQMessagesViewController中限制用户文本输入和禁用发送按钮

时间:2016-08-06 02:49:27

标签: swift jsqmessagesviewcontroller

我正在使用JSQMessagesViewController构建一个消息传递应用程序,我想知道是否有一种方法让我要求用户的文本输入长度在10到140个字符之间。

我已经放了以下这段代码

def assertInHTML(self, needle, haystack, count=None, msg_prefix=''):
    needle = assert_and_parse_html(self, needle, None,
        'First argument is not valid HTML:')
    haystack = assert_and_parse_html(self, haystack, None,
        'Second argument is not valid HTML:')
    real_count = haystack.count(needle)
    if count is not None:
        self.assertEqual(real_count, count,
            msg_prefix + "Found %d instances of '%s' in response"
            " (expected %d)" % (real_count, needle, count))
    else:
        self.assertTrue(real_count != 0,
            msg_prefix + "Couldn't find '%s' in response" % needle)

在几个关键点,例如函数if (text.characters.count <= 10 || text.characters.count >= 140) { self.inputToolbar.sendButtonOnRight = false } else { self.inputToolbar.sendButtonOnRight = true }

didPressSendButton

和我的override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) { let itemRef = messageRef.childByAutoId() // 1 let messageItem = [ // 2 "text": text, "senderId": senderId, "location": getLocation() ] itemRef.setValue(messageItem) // 3 // 4 JSQSystemSoundPlayer.jsq_playMessageSentSound() // 5 finishSendingMessage() Answers.logCustomEventWithName("Message sent", customAttributes: nil) } 功能:

addMessages
像这样:

func addMessage(id:String,text:String,displayName:String){

func addMessage(id: String, text: String, displayName: String) {
    let message = JSQMessage(senderId: id, displayName: displayName, text: text)
    messages.append(message)
}

}

然而,这不起作用。

在我的addMessages函数中,对我来说有什么用呢:

if (text.characters.count <= 10 || text.characters.count >= 140) {
    self.inputToolbar.sendButtonOnRight = false
}
else {
    self.inputToolbar.sendButtonOnRight = true
    let message = JSQMessage(senderId: id, displayName: displayName, text: text)
    messages.append(message)

}

在这种情况下,字符过滤器工作但发送按钮仍然启用,因此数据被发送到我不想要的数据库。如果发送按钮刚刚被禁用,如果文本输入不够长或太长,那对我来说会更好。

如果有人能够帮助我,那就太好了。谢谢!

1 个答案:

答案 0 :(得分:0)

您不应该从栏中删除该按钮,而只是禁用它。我首先建议为这个按钮设置一个禁用状态。

let sendButton = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 35)) //You can change these values to fit your needs
sendButton.setTitle("Send", forState: .Normal)
sendButton.setTitleColor(UIColor.grayColor(), forState: .Disabled)
sendButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
sendButton.contentMode = .Center
self.inputToolbar?.contentView?.rightBarButtonItem = sendButton

然后,您希望根据输入字段中字符串的长度在该按钮上设置userInteractionEnabled属性。我们可以通过设置观察者来实现这一点。

首先获取对inputField的引用。     让inputField = self.inputToolbar.contentView.inputView    然后在该字段上设置观察者并更改sendButtons状态。

NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: inputField, queue: NSOperationQueue.mainQueue()) { (notification) in
        let text = self.inputField.text

if语句周围不需要括号()。 我也会交换你的逻辑

if text.characters.count >= 10 && text.characters.count <= 140 {
        if text == self.verifyPasswordField.text {
            self.sendButton.enabled = true
        } else {
            self.sendButton.enabled = false
        }
    }

如果你想成为一个快速的忍者,你可以用它做速记。

self.sendButton.enabled = (text.characters.count >= 10 && text.characters.count <= 140)

把它放在一个函数中并确保你调用它,你应该好好去。如果输入文本不符合条件,Button应更新其颜色。但您也可能想告诉您的用户,这就是按钮被禁用的原因,但这只是我的意见。如果您有任何疑问,我希望这有助于我知道。