一个简短的问题:在注册过程中,我想要求用户从值列表中选择一个值.@H_403_3@
是否使用视图控制器添加所有文本字段和值为UIPickerVIEw的正确方法?由于选择器视图在文本区域之间需要如此大的空间,我不知道在这种情况下最好的做法是什么?@H_403_3@
这是我的代码到目前为止@H_403_3@
class RegisterVIEwController: UIVIEwController,UIPickerVIEwDelegate,UIPickerVIEwDataSource{@IBOutlet weak var gradeTextFIEld: UITextFIEld!var gradePicker: UIPickerVIEw!let gradePickerValues = ["5. Klasse","6. Klasse","7. Klasse"]func numberOfComponentsInPickerVIEw(pickerVIEw: UIPickerVIEw!) -> Int{ return 1}func pickerVIEw(pickerVIEw: UIPickerVIEw,numberOfRowsInComponent component: Int) -> Int{ return gradePickerValues.count}func pickerVIEw(pickerVIEw: UIPickerVIEw,TitleForRow row: Int,forComponent component: Int) -> String! { return gradePickerValues[row]}func pickerVIEw(pickerVIEw: UIPickerVIEw!,dIDSelectRow row: Int,inComponent component: Int){ gradeTextFIEld.text = gradePickerValues[row] self.vIEw.endEditing(true)}overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() statusMessageLabel.hIDden = true gradePicker = UIPickerVIEw() gradePicker.dataSource = self gradePicker.delegate = self gradeTextFIEld.inputVIEw = gradePicker gradeTextFIEld.text = gradePickerValues[0]}
选择器视图在开头隐藏,只有当我选择文本字段时才出现,这到目前为止还不错…但是选择器视图是空的…@H_403_3@解决方法 这取决于控制器的外观.如果每个屏幕只有一个选择动作,最好将表视图放在其上,选定的行将是当前选择.
如果屏幕有多个字段,该用户应该采取行动,那么在我看来,最好把标签按钮放在它上方,当用户按下这个按钮时,您只需从屏幕底部显示“Picker VIEw”.当用户在“Picker VIEw”中选择任何行时,您可以更改标签文本,但不要隐藏选择器本身,所以应该按上述“Done”按钮完成.@H_403_3@
希望这可以帮助.@H_403_3@
UPD:@H_403_3@
你的问题,因为你只是忘了设置UIPickerVIEw的dataSource属性@H_403_3@
只要做:gradePicker.dataSource = self在vIEwDIDLoad()@H_403_3@
不要忘了在这里实现协议:class RegisterVIEwController:UIVIEwController,UIPickerVIEwDataSource@H_403_3@
UPD 2:@H_403_3@
终于做到了如果您在textfiled的inputVIEw中添加UIPickerVIEw,那么它不应该在IB中.所以你可以从故事板(或.xib,如果你使用它)删除它.@H_403_3@
然后更改代码就是这样的:@H_403_3@
class RegisterVIEwController: UIVIEwController,UIPickerVIEwDataSource { @IBOutlet weak var gradeTextFIEld: UITextFIEld! var gradePicker: UIPickerVIEw! let gradePickerValues = ["5. Klasse","7. Klasse"] func numberOfComponentsInPickerVIEw(pickerVIEw: UIPickerVIEw) -> Int{ return 1 } func pickerVIEw(pickerVIEw: UIPickerVIEw,numberOfRowsInComponent component: Int) -> Int{ return gradePickerValues.count } func pickerVIEw(pickerVIEw: UIPickerVIEw,forComponent component: Int) -> String! { return gradePickerValues[row] } func pickerVIEw(pickerVIEw: UIPickerVIEw!,inComponent component: Int){ gradeTextFIEld.text = gradePickerValues[row] self.vIEw.endEditing(true) } overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() gradePicker = UIPickerVIEw() gradePicker.dataSource = self gradePicker.delegate = self gradeTextFIEld.inputVIEw = gradePicker gradeTextFIEld.text = gradePickerValues[0] }}总结
以上是内存溢出为你收集整理的ios swift:UIPickerView最佳做法?全部内容,希望文章能够帮你解决ios swift:UIPickerView最佳做法?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)