【问题标题】:UITextField unresponsive to opening UIPickerViewUITextField 对打开 UIPickerView 无响应
【发布时间】:2016-04-24 11:38:56
【问题描述】:

所以我使用 UIPickerView 作为 UITextField 的第一响应者。我将其设置为显示:

var myPickerView = UIPickerView()
myTextField.inputView = myPickerView

在我向视图添加 UITapGestureRecognizer(这将允许我在选择器视图之外单击以将其关闭)之前,我没有遇到此问题,如下所示:

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

hidePicker 函数:

@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 上单击五次才能打开选择器视图,这很令人沮丧,而且显然对应用程序不利。我很确定这与点击手势有关,但我可能是错的。

如果你们还有其他问题,请告诉我。

【问题讨论】:

    标签: ios swift uitextfield uipickerview uitapgesturerecognizer


    【解决方案1】:

    添加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)
        }
    

    【讨论】:

    • 对不起我的无知,但可以帮助指出我将如何实现这一点?我以前没用过这种东西。
    • 粘贴到你的viewController中!
    • 好吧,我试过了并禁用了 UITapGestureRecognizer 的东西,但是现在当我在 UIPickerView 外面点击时它不会关闭它,这是我需要的
    • 应该可以正常工作!你应该做错了什么!你能粘贴你的代码还是确保你完全删除UITapGestureRecognizer的代码
    • 我像你说的那样使用它,但它不起作用。你说我不需要做任何其他事情,所以你的答案对我不起作用。
    【解决方案2】:

    我建议您添加带有“完成”和“清除”项目的工具栏,而不是手势。 您可以使用以下代码:

        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()
    
        }
    

    【讨论】:

    • 我喜欢这个想法,我尝试使用您的代码,但它只是在我的选择器视图选项上方添加了额外的空间并且没有显示任何内容
    • 我通过将您的代码放在我的viewDidLoad 方法中解决了这个问题。我很抱歉,但我不认为我会接受你的回答,因为@Med Abida 已经在下面指导我这个想法,我不得不参考其他 Stack Overflow 问题才能让它正常工作。对不起:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2021-09-16
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多