答案 0 :(得分:1)
注意:如果您有一个必须自定义的视图控制器,则使用此选项...
试试这个:
第1步:在View Controller
添加此变量:
var chosenState = String()
第2步:将此功能添加到View Controller
:
override func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
chosenState = yourPickerData[row] // yourPickerData = your state array
}
第3步:如果您已将segue连接到该按钮,请将其删除。在故事板上做以下事情:
选择显示连接,点击segue并将其标识设置为customSegue
,如上图所示。
第4步:在视图控制器中的继续按钮功能中,添加:
self.performSegueWithIdentifier("customSegue", sender: nil)
第5步:在prepareForSegue
中添加ViewController
方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "customSegue" {
if let secondViewController = segue.destinationViewController as? secondViewController {
secondViewController.customLabel.text = self.chosenState
// this is an example, but the view will contain the name of the chosen state by the time the button gets clicked... You can customize it...
}
}
}
希望这有帮助!让我知道你的想法!
<方法2: 注意:如果您在故事板中创建了更多View Controllers
,则可以使用此功能。
试试这个:
第1步:转到Storyboard
并在pickerView
下方的场景中添加一个按钮,如下图所示,并确保该课程View Controller的实际设置为ViewController.swift
:
第2步:打开助手编辑器并通过 Ctrl 连接按钮+点击并拖动到ViewController.swift
,如下所示:
连接类型:操作,根据您的喜好命名。连接后,将显示:
@IBAction func continueButtonPressed(sender: AnyObject?){
//this is your continue button function
}
注意:对于教程目的,我只会与3个国家合作!
第3步:将三个View Controllers
(或任意多个)拖到场景中,然后添加三个(或任意多个)名为StateName View Controller的swift文件到你的项目。将此代码添加到他们:
import UIKit
class StateNameViewController: UIViewController{
}
第4步:分配他们的课程,如下所示:
选择ViewController
上的第一个storyboard
,转到 Identity Inspector 并将其类设置为AlabamaViewController
。和其他2一样。
第5步:然后为每个创建一个segue并将其标识符设置为State Name Segue,如下所示:
第6步:将pickerView
与代码连接起来:
STEP 7:转到ViewController并添加:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
var pickerData = ["Alabama","Alaska","Kansas"]
var chosenState = ""
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
第8步:在ViewController.swift
内,添加以下功能:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
//Called when the user changes the selection...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chosenState = pickerData[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
STEP 9:添加了代码的按钮功能:
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)Segue", sender: nil)
}
最后的ViewController.swift:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
var pickerData = ["Alabama","Alaska","Kansas"]
var chosenState = ""
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)Segue", sender: nil)
}
// The number of columns of data
// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
//Called when the user changes the selection...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chosenState = pickerData[row]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
}
就是这样!告诉我它是否有帮助!
答案 1 :(得分:0)
好的,首先,我希望你在用户选择状态后有一个继续按钮,因为如果你不能让用户对所选状态改变主意感到沮丧,无论如何,你可以抓住所选状态函数pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
,你将获得所选的行,你可以使用它作为变量来确定继续按钮中的下一个视图控制器,带有一个开关或类似这样的switch selectedIndex{
case 0:
performSegueWithIdentifier("Segue1", self)
default:
print("No valid index selected")
}
< / p>