Swift - 带有多个 UIPickers 的视图控制器。
Swift - View controller with several UIPickers.
我正在使用 swift 2.0(我也是 Swift 和 iOS 开发的新手)...
我想要一个有两个 UIPickers 的视图控制器。谁能建议如何做到这一点?
我的视图控制器class ...
import UIKit
class CreateSessionViewController : UIViewController, UIPickerViewDataSource,UIPickerViewDelegate {
//Outlets:
@IBOutlet var durationPicker: UIPickerView!
@IBOutlet var typePicker: UIPickerView!
//Dummy duration array:
var durationArray = ["10", "20", "30", "40", "50", "60"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.durationPicker.dataSource = self;
self.durationPicker.delegate = self;
}
//Picker view methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return durationArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return durationArray[row]
}
}
现在,durationPicker 运行良好,但我将如何填充 typePicker?
您应该使用传递给每个委托方法的 pickerView 对象:
//Picker view methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if pickerView == durationPicker {
return 1
} else if pickerView == typePicker {
return 2
}
return 0
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == durationPicker {
return durationArray.count
} else if pickerView == typePicker {
return typeArray.count
}
return 0
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == durationPicker {
return durationArray[row]
} else if pickerView == typePicker {
return typeArray[row]
}
return nil
}
你需要像这样在委托方法中过滤
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if durationPicker == pickerView {
return 1 // The amount that you need for the durationPicker
} else if typePicker == pickerView {
return 2 // The amount that you need for the typePicker
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if durationPicker == pickerView {
return durationArray.count
} else if typePicker == pickerView {
return typeArray.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if durationPicker == pickerView {
return durationArray[row]
} else if typePicker == pickerView {
return typeArray[row]
}
}
希望对您有所帮助!
我正在使用 swift 2.0(我也是 Swift 和 iOS 开发的新手)...
我想要一个有两个 UIPickers 的视图控制器。谁能建议如何做到这一点?
我的视图控制器class ...
import UIKit
class CreateSessionViewController : UIViewController, UIPickerViewDataSource,UIPickerViewDelegate {
//Outlets:
@IBOutlet var durationPicker: UIPickerView!
@IBOutlet var typePicker: UIPickerView!
//Dummy duration array:
var durationArray = ["10", "20", "30", "40", "50", "60"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.durationPicker.dataSource = self;
self.durationPicker.delegate = self;
}
//Picker view methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return durationArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return durationArray[row]
}
}
现在,durationPicker 运行良好,但我将如何填充 typePicker?
您应该使用传递给每个委托方法的 pickerView 对象:
//Picker view methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if pickerView == durationPicker {
return 1
} else if pickerView == typePicker {
return 2
}
return 0
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == durationPicker {
return durationArray.count
} else if pickerView == typePicker {
return typeArray.count
}
return 0
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == durationPicker {
return durationArray[row]
} else if pickerView == typePicker {
return typeArray[row]
}
return nil
}
你需要像这样在委托方法中过滤
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if durationPicker == pickerView {
return 1 // The amount that you need for the durationPicker
} else if typePicker == pickerView {
return 2 // The amount that you need for the typePicker
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if durationPicker == pickerView {
return durationArray.count
} else if typePicker == pickerView {
return typeArray.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if durationPicker == pickerView {
return durationArray[row]
} else if typePicker == pickerView {
return typeArray[row]
}
}
希望对您有所帮助!