我在主视图中的滚动视图中有2个子视图。基于滚动视图中的两个按钮,2sub视图将显示和隐藏。两个子视图都有一个文本字段,每个字段都输入数字。键盘是数字键盘。我已为两个子视图实施了func touchesBegan
和UITapGestureRecognizer
,并添加了resignFirstResponder()
和endEditing(true)
。但我的键盘仍然不会消失。我无法在任何论坛中找到适合此类场景的解决方案。
我正在使用Swift 2.0 xcode7.1.1
编辑:我的代码(因为这是一个商业项目,我不能分享完整内容对不起)
当我在键盘上的viewAutomatic
触摸外面时,在此代码中。但是当我在viewManual
外面触摸时键盘不会熄灭。这对我来说太奇怪了。
@IBOutlet weak var viewScroll: UIScrollView!
@IBOutlet weak var txtNewBid: UITextField!
@IBOutlet weak var txtMyMaxBid: UITextField!
@IBOutlet weak var viewManual: UIView!
@IBOutlet weak var viewAutomatic: UIView!
override func viewDidLoad() {
let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTapped:")
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.numberOfTouchesRequired = 1
singleTapGestureRecognizer.enabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
self.viewManual.addGestureRecognizer(singleTapGestureRecognizer)
self.viewAutomatic.addGestureRecognizer(singleTapGestureRecognizer)
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
func singleTapped(sender: UITapGestureRecognizer) {
self.view.endEditing(true)
self.viewManual.endEditing(true)
self.viewAutomatic.endEditing(true)
self.viewScroll.endEditing(true)
txtNewBid.resignFirstResponder()
txtMyMaxBid.resignFirstResponder()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
txtNewBid.resignFirstResponder()
txtMyMaxBid.resignFirstResponder()
}
func keyboardWillShow(notification: NSNotification) {
// adjusting the scroll view for the keyboard
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
if( self.viewManual.hidden == true)
{
self.viewScroll.frame.size.height -= frame.height
}
else
{
self.viewScroll.frame.size.height -= frame.height
}
let bottomOffset: CGPoint = CGPointMake(0, self.txtNewBid.frame.origin.y + self.txtNewBid.frame.size.height)
self.viewScroll.contentOffset = bottomOffset
}
func keyboardWillHide(notification: NSNotification) {
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
if( self.viewManual.hidden == true)
{
self.viewScroll.frame.size.height += frame.height
}
else
{
self.viewScroll.frame.size.height += frame.height
}
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
txtNewBid.text = ""
txtMyMaxBid.text = ""
}
答案 0 :(得分:1)
我的猜测是你正以错误的方式解决问题。我假设控制器应该负责解雇键盘,但你可能会尝试这样的东西(最好是你的托管视图控制器):
yourView.endEditing(true) // boolean parameter specifies whether to force dismissal or not
endEditing(force)
应该遍历视图层次结构并检查是否有任何活动的响应者,因此这应该可以正常工作。你检查过你是否确实引用了正确的视图吗?你能添加一些代码(请只是必要的部分)吗?
我之所以指出在托管控制器的视图中调用它是很重要的,它会遍历所有子视图并检查任何活动的响应者。
如果你有对文本字段的引用,并且你知道它是哪一个,你也可以调用
yourTextField.resignFirstResponder()
我猜这就是endEditing(force)
在内部执行的操作,它遍历所有子视图并在遇到的任何textField上调用resignFirstResponder()
,这也是第一个响应者。
由于UIScrollView
会截取手势,我会直接将点击手势识别器添加到滚动视图中,如下所示:
self.viewScroll.addGestureRecognizer(singleTapGestureRecognizer)
然后做这样的事情:
func singleTapped(sender: UITapGestureRecognizer) {
// this should suffice
self.viewScroll.endEditing(true)
// or call it on the touched view (in this case the viewScroll)
// sender.view.endEditing(true)
}