我在表格单元格中使用文本视图。其他单元格与segue相关联,因此我需要以下行为:
(默认)点击其他表格行会触发相关的segue
在文本视图中点击开始编辑并显示键盘
如果键盘可见,则在文本视图外部点击会取消键盘,但不会跟随segues
一旦键盘被解除,随后点击segues。
答案 0 :(得分:3)
<强> 1。采用textView
委托方法和
textView.delegate = self
<强> 2。添加全局属性
var tap = UITapGestureRecognizer()
第3。实施textView
委托方法
func textViewDidBeginEditing(textField: UITextView) {
self.tableView.allowsSelection = false
tap = UITapGestureRecognizer(target: self, action: #selector(self.Tapped))
self.view.addGestureRecognizer(tap)
}
func textViewDidEndEditing(textField: UITextView) {
self.tableView.allowsSelection = true
self.view.removeGestureRecognizer(tap)
}
<强> 4。实施Tapped()
func Tapped(){
view.endEditing(true)
}
注意:这也适用于UITextField,在整个
中替换View with Field答案 1 :(得分:0)
将UITextViewDelegate协议添加到您的类并添加这些变量
var textViewIsEditing:Bool = false
var tap: UITapGestureRecognizer!
覆盖viewdidload函数,以便tableViewController从文本视图中侦听事件。
还要添加一个UITapGestureRecognizer,它将触发自定义handleTap函数
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self
tap = UITapGestureRecognizer(target: self, action: #selector(MyClass.handleTap))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
}
添加两个函数以根据是否正在编辑textview来切换textViewIsEditing
func textViewDidEndEditing(textView: UITextView) {
textViewIsEditing = false
}
func textViewDidBeginEditing(textView: UITextView) {
textViewIsEditing = true
}
现在添加一个自定义handleTap函数来决定如何处理点击:
func handleTap(thisGestureRecognizer:UITapGestureRecognizer){
if(textFieldIsEditing){
// ignore this tap if editing text field but end editing
tap.cancelsTouchesInView = true
view.endEditing(true)
}else{
// proceed as normal
tap.cancelsTouchesInView = false
}
}