我为UIPickerView
创建了一个工具栏:
override func viewDidLoad() {
pickerToolbar.barStyle = UIBarStyle.Default
pickerToolbar.translucent = true
pickerToolbar.tintColor = UIColor(red: 0.0, green: 112/255, blue: 186/255, alpha: 1.0)
pickerToolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "hidePicker()")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "hidePicker()")
pickerToolbar.setItems([cancelButton, spaceButton, doneButton], animated: false)
pickerToolbar.userInteractionEnabled = true
typePicker.addSubview(pickerToolbar)
}
func hidePicker() {
typePicker.hidden = true
}
但是当我点击“完成”或“取消”按钮时,选择器会选择第一行并关闭。我不想要这样的行为,我只是想隐藏选择器并保留选定的值。
所以,我有两个问题:
UIPickerView
表现得像这样?感谢您的帮助!
答案 0 :(得分:0)
我发现你有两个错误。一个是行动错字。它必须是动作:“hidePicker”,根据您的代码没有括号。
第二个是你的pickerToolbar不应该是pickerView的子视图。将其更改为 self.view.addSubview(pickerToolbar),您将看到更改。
干杯,