gpt4 book ai didi

ios - UITextField Swift 中 UIPicker 的 resignFirstResponder

转载 作者:IT王子 更新时间:2023-10-29 05:27:06 29 4
gpt4 key购买 nike

我希望在单击工具栏上的完成按钮后关闭 UIPickerView(滑出 View )。我已将 UIPickerView 链接为 UITextField 的 inputView,并将 UIToolbar 设置为 UIPickerView 的 inputAccessoryView。但是,我调用 resignFirstResponder 的函数不会将 UIPicker 滑出 View 。感谢您提供任何帮助,并提前致谢!

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]
}
}

最佳答案

有两个问题:

  1. donePressed 的选择器声明不正确。应该是:

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

    func donePressed() {
    textField.resignFirstResponder()
    }

如果您有多个文本字段,并且不想在 resignFirstResponder 调用中对文本字段进行硬编码,您可以执行以下操作:

  1. 将您的 ViewController 设为 UITextFieldDeletate:

    class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
  2. UITextField 上设置 delegate 属性:

    textField.delegate = self
  3. 将属性添加到您的 ViewController 以跟踪事件的 UITextField:

    var activeTextField:UITextField?
  4. 实现 textFieldDidBeginEditing 并存储事件的 UITextField:

    func textFieldDidBeginEditing(textField: UITextField) { // became first responder
    activeTextField = textField
    }
  5. donePressed中,调用activeTextField上的resignFirstResponder:

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

关于ios - UITextField Swift 中 UIPicker 的 resignFirstResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573157/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com