我目前正在尝试为UIPickerView创建一个完成按钮。
我相信我已经在代码中添加了一个完成按钮来显示但是完成按钮没有显示在模拟器中。
这是我的代码:
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.
}
}
这么多!
答案 0 :(得分:0)
将工具栏分配给textFieldShouldBeginEditing中的textfield而不是IBAction。 preferenceTextClick(_:UITextField)可能会晚于此调用,因此不会加载工具栏。