如何在SWIFT中忽略文本视图中的键盘

时间:2015-06-30 16:40:31

标签: ios swift uitextview

我正在尝试使用带有完成按钮的附件视图在文本视图中关闭键盘但是键盘没有显示完成按钮。实际上我使用inputAccessoryView在textViewDidBeginEditing中编写了我的代码。这是我的代码。

func textViewDidBeginEditing(textView:UITextView){         currentTextView = textView //这是告诉文本视图中的当前位置

    var indexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 2)
    let cell:EventTableTableViewCell = EventTableview.cellForRowAtIndexPath(indexPath) as! EventTableTableViewCell
    cell.messageTextView.autocorrectionType = UITextAutocorrectionType.No


    let keyboardDoneButton = UIToolbar()
    keyboardDoneButton.sizeToFit()

    let item = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("doneButton"))



    var toolbarButtons = [item]
    //Put the buttons into the ToolBar and display the tool bar
    keyboardDoneButton.setItems(toolbarButtons, animated: false)
        keyboardDoneButton.userInteractionEnabled = true

//cell.messageTextView.inputAccessoryView = keyboardDoneButton
    textView.inputAccessoryView = keyboardDoneButton

    }


}
func doneButton()
{
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)

}

2 个答案:

答案 0 :(得分:0)

_textView.returnKeyType = UIReturnKeyType.Done

答案 1 :(得分:0)

你在IB中设置了键盘吗? 故事板> Textfield>属性检查器>返回键>完成

实施委托方法:

- textfieldShouldReturn:

在此处找到:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldReturn