为 UIPickerView 创建一个完成按钮

Posted

技术标签:

【中文标题】为 UIPickerView 创建一个完成按钮【英文标题】:Create a done button for UIPickerView 【发布时间】:2015-06-14 22:51:26 【问题描述】:

我目前正在尝试。

我相信我已经在代码中添加了一个完成按钮以显示但一个完成按钮没有显示在模拟器中。

这是我的代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UIPickerViewDataSource 

    @IBOutlet weak var genderTextField: UITextField!
    @IBOutlet weak var getGender: UIPickerView!

    @IBOutlet weak var preferenceTextField: UITextField!
    @IBOutlet weak var getPreference: UIPickerView!

    let gender = ["", "Male", "Female"]
    let preference = ["", "Male", "Female", "Both"]

    override func viewDidLoad() 
        super.viewDidLoad()

        var theWidth = view.frame.size.width
        var elWidth = theWidth - 50

        // Adds title text field to the top
        var titleLabel: UILabel = UILabel()
        titleLabel.frame = CGRectMake(25, 40, elWidth, 40)
        titleLabel.textColor = UIColor(red: 1.0, green: (203.0/255.0), blue: (38.0/255.0), alpha: 1.0)
        titleLabel.font = UIFont(name: "Apple SD Gothic Neo", size: 28.0)
        titleLabel.textAlignment = NSTextAlignment.Center
        titleLabel.text = "Set up your profile"
        self.view.addSubview(titleLabel)

        getGender.hidden = true;
        genderTextField.placeholder = "Gender"
        genderTextField.delegate = self
        getGender.delegate = self
        getGender.frame = CGRectMake(0,view.frame.size.height-216, view.frame.size.width, 216)
        getPreference.hidden = true;
        preferenceTextField.placeholder = "Gender preference"
        preferenceTextField.delegate = self
        getPreference.delegate = self
        getPreference.frame = CGRectMake(0,view.frame.size.height-216, view.frame.size.width, 216)

        var toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
        toolBar.sizeToFit()

        var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
        var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "canclePicker")

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

        genderTextField.inputView = getGender
        genderTextField.inputAccessoryView = toolBar

        preferenceTextField.inputView = getPreference
        preferenceTextField.inputAccessoryView = toolBar


    


    @IBAction func genderTextClick(sender: UITextField) 

        getGender.showsSelectionIndicator = true

        // Create toolbar
        var toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
        toolBar.sizeToFit()

        // Create buttons
        var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneGenderPicker")
        var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelGenderPicker")

        // Assign buttons to toolbar
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.userInteractionEnabled = true

        // Add pickerview and toolbar to textfield
        genderTextField.inputView = getGender
        genderTextField.inputAccessoryView = toolBar

        sender.inputView = getGender

    


    @IBAction func preferenceTextClick(sender: UITextField) 



        getPreference.showsSelectionIndicator = true

        // Create toolbar
        var toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
        toolBar.sizeToFit()

        // Create buttons
        var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePreferencePicker")
        var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPreferencePicker")

        // Assign buttons to toolbar
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.userInteractionEnabled = true

        // Add pickerview and toolbar to textfield
        preferenceTextField.inputView = getPreference
        preferenceTextField.inputAccessoryView = toolBar

        sender.inputView = getPreference

    


    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
        return 1
    

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 

        getGender.tag = 0
        getPreference.tag = 1

        if pickerView.tag == 0 
            return gender.count
         else if pickerView.tag == 1 
            return preference.count
        
        return 1
    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 

        getGender.tag = 0
        getPreference.tag = 1

        if pickerView.tag == 0 
            return gender[row]
         else if pickerView.tag == 1 
            return preference[row]
        
        return ""
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  

        getGender.tag = 0
        getPreference.tag = 1

        if pickerView.tag == 0 
            genderTextField.text = gender[row]
//            getGender.hidden = true
         else if pickerView.tag == 1 
            preferenceTextField.text = preference[row]
//            getPreference.hidden = true
        
    

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool 
        if (textField == self.genderTextField) 
            getGender.hidden = false
         else if (textField == self.preferenceTextField) 
            getPreference.hidden = false
        
        return false
    

    func doneGenderPicker() 
        genderTextField.resignFirstResponder()
    
    func cancelGenderPicker() 
        genderTextField.resignFirstResponder()
    

    func donePreferencePicker() 
        preferenceTextField.resignFirstResponder()
    
    func cancelPreferencePicker() 
        preferenceTextField.resignFirstResponder()
    


//    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 
//        
//        getGender.hidden = true
//        getPreference.hidden = true
//    

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


太多了!

【问题讨论】:

删除带有“谢谢”的行。而是赞成并接受有帮助的答案 删除空格按钮后会发生什么情况,doneButton 是否显示在 cancelButton 旁边?或者如果你把它改成toolBar.setItems([doneButton, spaceButton, cancelButton], animated: false),cancelButton 会消失吗? @dstudeba 一个按钮都没有出现,我给你看个uipicker的截图。 @Aditya 如果你想看,我也改了谢谢 【参考方案1】:

将工具栏分配给 textFieldShouldBeginEditing 中的文本字段,而不是 IBAction。 preferenceTextClick(_: UITextField) 可能会在此之后被调用,因此不会加载工具栏。

【讨论】:

以上是关于为 UIPickerView 创建一个完成按钮的主要内容,如果未能解决你的问题,请参考以下文章

UIActionSheet 中的 UIPickerView:UI 冻结

如何使用完成按钮确认我在 UIPickerView 中的选择?

带有完成按钮的 UIPickerView 不起作用

UIPickerview 完成按钮不起作用,即使它没有调用选择器方法

Swift - UIPickerView 子视图中没有出现工具栏完成按钮

在 UITableView 中隐藏 UIPickerview 完成按钮