@IBOutlet weak var picker: UIPickerView!
let beauties = ["范冰冰","李冰冰","王菲","杨幂","周迅"]
picker.delegate = self as UIPickerViewDelegate
picker.dataSource = self
//页面跳转的方法(xib)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "GoToGallery" {
let index = picker.selectedRow(inComponent: 0)
var imageName: String?
switch index{
case 0:
imageName = "fanbingbing"
case 1:
imageName = "libingbing"
case 2:
imageName = "wangfei"
case 3:
imageName = "yangmi"
case 4:
imageName = "zhouxu"
default:
imageName = nil
}
let vc = segue.destination as! GalleryViewController
vc.imageName = imageName
}
//页面返回
@IBAction func close(segue: UIStoryboardSegue) {
}
}
新建文件viewcontrollerextension()
extension ViewController : UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return beauties.count
}
}
extension ViewController : UIPickerViewDelegate{
public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
return beauties[row]
}
}