【发布时间】:2016-09-01 18:19:40
【问题描述】:
我正在使用 Swift 在 XCode 中编写一个应用程序,我目前在应用程序上有一个列表选择器。我希望用户能够从选择器视图中选择一个状态,这会将他们带到一个新屏幕(专为该特定状态而设计)。
用户界面:
【问题讨论】:
标签: ios iphone swift uipickerview picker
我正在使用 Swift 在 XCode 中编写一个应用程序,我目前在应用程序上有一个列表选择器。我希望用户能够从选择器视图中选择一个状态,这会将他们带到一个新屏幕(专为该特定状态而设计)。
用户界面:
【问题讨论】:
标签: ios iphone swift uipickerview picker
注意:如果您有一个必须自定义的视图控制器,则可以使用它...
试试这个:
第 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 步: 在视图控制器的 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...
}
}
}
希望这会有所帮助!让我知道你的想法!
注意:如果您在情节提要中创建了更多View Controllers,则可以使用它。
试试这个:
步骤 1: 转到您的 Storyboard 并在场景中添加一个按钮,在 pickerView 下方,如下图所示,并确保您的 View Controller 的类实际上设置为ViewController.swift:
第二步:打开助手编辑器,通过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 步:分配他们的班级,如下所示:
选择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)
}
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
}
}
就是这样!告诉我是否有帮助!
【讨论】:
好的,首先,我希望您在用户选择状态后有一个继续按钮,因为如果不这样做,您可能会让用户对所选状态改变主意感到沮丧,无论如何,您可以通过以下方式捕捉所选状态函数pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int),通过这个你将选择行,你可以将它用作变量来确定继续按钮中的下一个视图控制器,使用开关或类似switch selectedIndex{
case 0:
performSegueWithIdentifier("Segue1", self)
default:
print("No valid index selected")
}
【讨论】: