简体   繁体   English

UITextField Swift中UIPicker的resignFirstResponder

[英]resignFirstResponder for UIPicker in UITextField Swift

I want the UIPickerView to be dismissed (slide out of view) after the done button is clicked on the toolbar. 我想在工具栏上单击完成按钮后关闭UIPickerView(滑出视图)。 I've linked the UIPickerView as the inputView of the UITextField and set the UIToolbar as the inputAccessoryView of the UIPickerView. 我已将UIPickerView链接为UITextField的inputView,并将UIToolbar设置为UIPickerView的inputAccessoryView。 However, my function calling resignFirstResponder does not slide the UIPicker out of the view. 但是,我调用resignFirstResponder的函数不会将UIPicker滑出视图。 Any help is appreciated and thanks in advance! 任何帮助表示赞赏,并提前致谢!

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    // Do any additional setup after loading the view, typically from a nib.

    //initialization constants
    let pickerView = UIPickerView()
    let textField = UITextField()
    let pickerData = ["1","2","three"]


    override func viewDidLoad() {
        //pickerview tool bar
        let toolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
        var items = [AnyObject]()
        //making done button
        let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: Selector(donePressed()))
        items.append(doneButton)
        toolbar.barStyle = UIBarStyle.Black
        toolbar.setItems(items, animated: true)


        //creating textfields with a pickerview
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.frame = CGRectMake(0, 0, 500, 300)
        textField.inputAccessoryView = toolbar
        textField.inputView = pickerView
        textField.frame = CGRectMake(200, 55, 100, 35)
    textField.backgroundColor = UIColor.blueColor()

    //adding objs to viewController
    self.view.addSubview(textField)
}

func donePressed() {
    resignFirstResponder()
}





override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

//MARK: Data Sources UIPickerView
extension ViewController: UIPickerViewDataSource {
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }
}

//MARK: Delegates UIPickerView
extension ViewController: UIPickerViewDelegate {
    // several optional methods:




func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return pickerData[row]
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
    textField.text = pickerData[row]
}
}

There are 2 problems: 有两个问题:

  1. Your selector for donePressed is declared incorrectly. 您的donePressed选择器声明不正确。 It should be: 它应该是:

     let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "donePressed") 
  2. You need to call resignFirstResponder() on textField : 您需要在textField上调用resignFirstResponder()

     func donePressed() { textField.resignFirstResponder() } 

If you have multiple textFields, and you don't want to hardcode the textField in the resignFirstResponder call, you can do the following: 如果您有多个textField,并且您不想在resignFirstResponder调用中对textField进行硬编码,则可以执行以下操作:

  1. Make your ViewController a UITextFieldDeletate : 使您的ViewController成为UITextFieldDeletate

     class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate { 
  2. Set the delegate property on your UITextField : UITextField上设置delegate属性:

     textField.delegate = self 
  3. Add a property to your ViewController to keep track of the active UITextField : ViewController添加属性以跟踪活动的UITextField

     var activeTextField:UITextField? 
  4. Implement textFieldDidBeginEditing and store the active UITextField : 实现textFieldDidBeginEditing并存储活动的UITextField

     func textFieldDidBeginEditing(textField: UITextField) { // became first responder activeTextField = textField } 
  5. In donePressed , call resignFirstResponder on activeTextField : donePressed ,呼吁resignFirstResponderactiveTextField

     func donePressed() { activeTextField?.resignFirstResponder() } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM