在视图选择器中选择状态,转到新屏幕

时间:2016-08-31 19:31:09

标签: ios iphone swift uipickerview picker

我正在使用Swift在XCode中编写应用程序,我目前在应用程序上有一个列表选择器。我希望用户能够从选择器视图中选择一个状态,这将使他们进入一个新的屏幕(为该特定状态设计)。

用户界面:

enter image description here

2 个答案:

答案 0 :(得分:1)

<方法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连接到该按钮,请将其删除。在故事板上做以下事情:

enter image description here

enter image description here

选择显示连接,点击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,则可以使用此功能。

试试这个:

INTERAFACE:

第1步:转到Storyboard并在pickerView下方的场景中添加一个按钮,如下图所示,并确保该课程View Controller的实际设置为ViewController.swiftenter image description here

第2步:打开助手编辑器并通过 Ctrl 连接按钮+点击并拖动到ViewController.swift,如下所示:

enter image description here enter image description here

连接类型:操作,根据您的喜好命名。连接后,将显示:

@IBAction func continueButtonPressed(sender: AnyObject?){
//this is your continue button function
}

注意:对于教程目的,我只会与3个国家合作!

第3步:将三个View Controllers(或任意多个)拖到场景中,然后添加三个(或任意多个)名为StateName View Controller的swift文件到你的项目。将此代码添加到他们:

import UIKit

    class StateNameViewController: UIViewController{

    }

1: enter image description here  2: enter image description here  3: enter image description here  4: enter image description here

第4步:分配他们的课程,如下所示:

选择ViewController上的第一个storyboard,转到 Identity Inspector 并将其类设置为AlabamaViewController。和其他2一样。

第5步:然后为每个创建一个segue并将其标识符设置为State Name Segue,如下所示:

如何为第一个做到这一点: enter image description here 另外2: enter image description here

第6步:将pickerView与代码连接起来:

enter image description here

CODE:

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)

}

预览 - 方法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
    }


}

就是这样!告诉我它是否有帮助!

答案 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>