UIBarbuttonitem选择器永远不会被触发(Swift 3)

时间:2017-01-19 08:18:14

标签: ios swift

有时最简单的事情证明最难解决!当用户选择用户位置文本字段但选择器不工作时,将显示选择器。通过SO和Apple文档已经消失,但无法理解为什么会这样。

let locationPicker = UIPickerView()
var locationData = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Set textfield delegate
    userID.delegate = self
    userLocation.delegate = self

    // Set up picker view for location selection
    locationPicker.frame = CGRect(x:0, y: self.view.bounds.height, width: self.view.bounds.width, height: 140)
    locationPicker.showsSelectionIndicator = true

    let doneButton = UIBarButtonItem(title: "done", style: .plain, target: self, action: #selector(self.closePicker(sender:)))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "cancel", style: .plain, target: self, action: #selector(self.closePicker(sender:)))

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.blackOpaque
    toolBar.isTranslucent = true

    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    userLocation.inputView = locationPicker
    userLocation.inputAccessoryView = toolBar

    // Load locations into array
    let ref: FIRDatabaseReference = FIRDatabase.database().reference().child("venues")
    ref.observe(.value, with: { snapshot in

        // Pull out the keys to represent locations
        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
            for snap in snapshots {
                print("snap key, \(snap.key) and snap value \(snap.value)")
                let loc : String = snap.key
                self.locationData.append(loc)
            }
        }

        // Now stick the array of locations into the picker view
        self.locationPicker.dataSource = self
        self.locationPicker.delegate = self
        self.locationPicker.reloadAllComponents()
    })
}

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

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.endEditing(true)
    return false
}

func closePicker(sender: AnyObject) {

    print("Close picker")
    self.locationPicker.isHidden = true
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    if let iText = userID.text,  let lText = userLocation.text, !iText.isEmpty, !lText.isEmpty
    {
        print("We have data")
        // loginButton.isUserInteractionEnabled = true
        // loginButton.alpha = 1.0
    }
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return locationData.count
}

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    userLocation.text = locationData[row]
}

0 个答案:

没有答案