我正在为iOS应用程序的注册视图控制器工作。我有UITextField
个用户输入性别,UIPickerView
为inputView
。
目前,我正在尝试将UIToolbar
添加为inputAccessoryView
。 UIToolbar
工作正常,但我在添加" Next"按钮。该按钮在屏幕上正确显示,但在点击时无法执行指定的操作。
@IBOutlet var genderTextField: UITextField!
var genderPicker:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
// set up the genderPicker
genderPicker.delegate = self
genderPicker.dataSource = self
// set up the genderToolbar
var genderToolbar = UIToolbar()
var genderDoneButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Bordered, target: self, action: "doneWithGender:")
genderToolbar.items = [genderDoneButton]
// set inputView and inputAccessoryView for genderTextField
genderTextField.inputView = genderPicker
genderTextField.inputAccessoryView = genderToolbar
}
func doneWithGender(sender:AnyObject){
println("gender button tapped")
}
我已遵循此处的建议:How to set the action for a UIBarButtonItem in Swift并在此处:How to fire the action of a uibarbuttonitem programatically?
非常感谢任何帮助!
答案 0 :(得分:4)
您的工具栏框架看起来像是(0,0,0,0),因此您的按钮会显示但不响应用户互动。
尝试这样的事情:
var genderToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 44))