1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import UIKit
protocol GenderPickerViewDelegate: NSObjectProtocol { func genderPickerDidSelectItem(row: Int) }
class GenderPickerView: UIView, UIPickerViewDataSource, UIPickerViewDelegate { var delegate: GenderPickerViewDelegate! var toolbar: UIToolbar! var picker: UIPickerView! var genderPickerViewData = ["不告诉你", "男", "女"] var genderSelectedRow = 0
override init(frame: CGRect) { super.init(frame: frame) toolbar = UIToolbar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20)) toolbar.barStyle = .Default toolbar.sizeToFit()
let cancelButton = UIBarButtonItem.init(title: "取消", style: UIBarButtonItemStyle.Done, target: self, action: "genderPickerDidCancel") let flexibleButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil) let doneButton = UIBarButtonItem.init(title: "确定", style: UIBarButtonItemStyle.Done, target: self, action: "genderPickerDidSelect") toolbar.setItems([cancelButton, flexibleButton, doneButton], animated: true)
picker = UIPickerView.init(frame: CGRectMake(0, 44, UIScreen.mainScreen().bounds.width, 120)) picker.dataSource = self picker.delegate = self
addSubview(toolbar) addSubview(picker) }
convenience init() { self.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.height - 164, UIScreen.mainScreen().bounds.width, 164)) }
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 }
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return 3 }
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return genderPickerViewData[row] }
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { genderSelectedRow = row }
func genderPickerDidSelect() { delegate?.genderPickerDidSelectItem(genderSelectedRow) removeFromSuperview() }
func genderPickerDidCancel() { removeFromSuperview() } }
|