我正在使用 Swift 在 XCode 中编写一个应用程序,目前我在该应用程序上有一个列表选择器。我希望用户能够从选择器 View 中选择一个状态,这会将他们带到一个新屏幕(专为该特定状态设计)。
用户界面:
最佳答案
方法一:
注意:如果您有一个必须自定义的 View Controller ,则可以使用它...
试试这个:
第 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 连接到按钮,请将其删除。在 Storyboard上做以下事情:
选择显示连接,点击segue并将其标识符设置为customSegue
,如上图所示。
第 4 步:在 View Controller 的continue 按钮函数中,添加:
self.performSegueWithIdentifier("customSegue", sender: nil)
第 5 步:在您的 ViewController
中添加 prepareForSegue
方法:
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...
}
}
}
希望对您有所帮助!让我知道你的想法!
方法二:
注意:如果您在 Storyboard 中创建了更多View Controllers
,将使用它。
试试这个:
界面:
第 1 步:转到您的 Storyboard
并在 pickerView
下方的场景中添加一个按钮,如下图所示,并确保您的 View Controller 的类实际上设置为 ViewController.swift
:
第 2 步:打开 Assistant Editor 并通过 Ctrl+Click 连接按钮并拖动到 ViewController.swift
,如如下所示:
连接类型:Action,随便命名。连接后,会出现:
@IBAction func continueButtonPressed(sender: AnyObject?){
//this is your continue button function
}
注意:出于教程目的,我将只使用 3 个状态!
第 3 步: 将三个 View Controllers
(或任意多个)拖到场景中,然后添加三个(或任意多个)swift 文件为您的项目调用 StateName View Controller。将此代码添加到它们:
import UIKit
class StateNameViewController: UIViewController{
}
第 4 步:分配他们的类(class),如下所示:
选择 storyboard
上的第一个 ViewController
,转到 Identity Inspector 并将其类设置为 AlabamaViewController
.其他 2 个也是如此。
第 5 步:然后为每个创建一个 segue,并将其标识符设置为 State Name Segue,如下所示:
第 6 步:使用如下代码连接 pickerView
:
代码:
第 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
}
第 9 步: 添加了代码的按钮函数:
@IBAction func continueButtonPressed(_ sender: AnyObject) {
self.performSegue(withIdentifier: "\(chosenState)Segue", sender: nil)
}
预览 - 方法 2:
最后的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
}
}
就是这样!告诉我是否有帮助!
关于ios - 在 View 选择器中选择状态,转到新屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39257616/