2016-04-24 105 views
-1

所以我用一个UIPickerView作为一个的UITextField的第一个响应者。我设置为显示:的UITextField反应迟钝打开UIPickerView

var myPickerView = UIPickerView() 
myTextField.inputView = myPickerView 

我是不是有这个问题,直到我添加了一个UITapGestureRecognizer(这将让我点击选择器视图之外开除吧)认为,像这样:

let dismissUnitPickerGesture = UITapGestureRecognizer(target: self, action: "hidePicker") 
dismissUnitPickerGesture.delegate = self 
dismissUnitPickerGesture.cancelsTouchesInView = false 

hidePicker FUNC:

@IBAction func hidePicker(){ 
    myTextField.resignFirstResponder() 

    //I don't think this code is relevant to the problem, but I included it just in case 
    if materialSelectedOption == "Aluminum Cans" { 
     amountTextField.placeholder = "Number of Cans" 
     unitCell.hidden = true 
    } 
} 

现在它占用的的UITextField 5次点击模拟器上打开选择器视图,这是令人沮丧的,显然不利于一个AP页。我很确定它与轻击手势有关,但我可能是错的。

如果您还有其他任何问题,请让我知道。

回答

0

添加UITapGestureRecognizer只是检测一个水龙头注册pickerView外面躲这是一种错误的! 可以使用touchesBegan功能和呼叫self.view.endEditing(true)像这样:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self.view.endEditing(true) 
     //or use textField.endEditing(true) 
    } 
+0

对不起,我的无知,但可以帮助指出我将如何实现这一点?我以前没有使用过任何类型的东西。 –

+0

将其粘贴到您的viewController中! –

+0

好吧,我想,和禁用UITapGestureRecognizer的东西,但现在并没有消除,当我点击它的外面,这是我需要 –

0

我建议你补充工具栏与“完成”和“清除”的项目,而不是姿态。 下面的代码,你可以使用:

func textFieldDidBeginEditing(sender: UITextField) {{ 
     let toolBar = UIToolbar(frame: CGRectMake(0, self.myTextField.frame.size.height/6, self.myTextField.frame.size.width, 40.0)) 
     toolBar.layer.position = CGPoint(x: self.myTextField.frame.size.width/2, y: self.myTextField.frame.size.height-20.0) 
     toolBar.barStyle = UIBarStyle.BlackTranslucent  
     toolBar.tintColor = UIColor.whiteColor() 


     let defaultButton = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(CustomTimePickerTextView.clearTime(_:))) 
     let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(CustomTimePickerTextView.donePressed(_:)))  
     let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil) 
     let textButton = UIBarButtonItem(title: self.placeholder, style: UIBarButtonItemStyle.Plain, target: nil, action: nil) 

     toolBar.setItems([defaultButton,flexSpace,textButton,flexSpace,doneButton], animated: true) 

     self.myTextField.inputAccessoryView = toolBar 
    } 

    func clearTime(sender:UIBarButtonItem) { 
     // 
    } 

    func donePressed(sender: UIBarButtonItem) { 
     self.myTextField.resignFirstResponder() 

    } 
+0

我喜欢这个想法,我试图使用你的代码,但它只是在我的选择器视图选项上面增加了额外的空间,并没有显示任何东西 –

+0

我通过将你的代码放在我的'viewDidLoad'方法。我很抱歉,但我不认为我会接受你的回答,因为@Med Abida已经指示我了解下面这个想法,而且我不得不引用其他Stack Overflow问题来使其工作。对不起:/ –

相关问题