简体   繁体   English

在Swift视图控制器之间传递数据不起作用的第一个Segue过渡

[英]Passing data between Swift view controllers not working on first segue transition

I have a Swift file containing four separate arrays of tuples. 我有一个Swift文件,其中包含四个独立的元组数组。 In ViewControllerA I have four different buttons, each of which changes the value of "selectedPosition" and is set to trigger "SegueAtoB". 在ViewControllerA中,我有四个不同的按钮,每个按钮都会更改“ selectedPosition”的值,并设置为触发“ SegueAtoB”。 "selectedPosition" should then be used in ViewControllerB to select the appropriate array to populate a UIPickerView on ViewControllerB. 然后应在ViewControllerB中使用“ selectedPosition”来选择适当的数组,以在ViewControllerB上填充UIPickerView。

Currently, my prepare for segue method only passes the correct data the second time a button is pressed, eg when the app loads into ViewControllerA I press a button, it takes me to ViewControllerB, but the array selected for the UIPickerView is the default, when I dismiss back to ViewControllerA and press a button, the code works as it should. 当前,我的“准备segue”方法仅在第二次按下按钮时才传递正确的数据,例如,当应用程序加载到ViewControllerA中时,我按下一个按钮,它将带我到ViewControllerB,但是为UIPickerView选择的数组是默认的,当我退回到ViewControllerA并按一个按钮,代码按预期工作。

ViewControllerA : ViewControllerA:

class ViewControllerA: UIViewController {

    // Variable to hold position selected (passed through to ViewControllerB)
    var selectedPosition : String = ""


    // Button1
    // Set up to run "SegueAtoB"
    @IBOutlet weak var selectButton_CEO: UIButton!
    @IBAction func selectButton_CEO_Tapped(_ sender: UIButton) {
        selectedPosition = "CEO"
    }


    // MARK: - Navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "SegueAtoB" {
            let VC : ViewControllerB = segue.destination as! ViewControllerB
            VC.ReceivePlayerDataDelegate = self
            VC.currentPosition = selectedPosition
        }
    }

}

ViewControllerB : ViewControllerB:

class ViewControllerB: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


    // set the position passed through from ViewControllerB
    var currentPosition : String = ""

    // set the playersArray by position
    var posArray = ceoArray


    // Button to dismiss ViewControllerB
    @IBAction func closeButton(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }


    // Picker view
    @IBOutlet weak var posPickerView: UIPickerView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // select appropriate array based on position selected
        selectArray(currentPos: currentPosition)

        // Delegate picker view to self
        posPickerView.delegate = self
        posPickerView.dataSource = self

    }


    // UIPickerViewMethods
    // Set the number of picker columns to 1
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    // Set the number of picker rows to the number of items in posArray
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return posArray
    }

    // Set the title of each picker row to each name in posArray
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return posArray[row].name
    }


    // Picker action
    // What happens when selection is made
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        print(posArray[row])

        // I've removed what's in here as it's not relevant to the question

    }


    // Method to select correct array based on currentPosition -- called in viewDidLoad
    func selectArray(currentPos: String) {

        switch (currentPos) {
        case "CEO" :
            posArray = ceoArray
        case "CFO" :
            posArray = cfoArray
        default :
            posArray = ceoArray
        }

    }


}

I need the UIPickerView to be updated to the relevant array on the first segue from ViewControllerA to ViewControllerB, not resorting to default on the first segue. 我需要在从ViewControllerA到ViewControllerB的第一个序列中将UIPickerView更新为相关的数组,而不是在第一个序列中使用默认值。

If multiple controls result in the same segue, you need to check the sender in the prepare(for:) function. 如果多个控件导致相同的segue,则需要在prepare(for:)函数中检查sender If it was the CEO button, set the CEO string etc.: 如果是CEO按钮,则设置CEO字符串等:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard segue.identifier == "SegueAtoB",
          let vc = segue.destination as? ViewControllerB,
          let senderButton = sender as? UIButton   else { return }

    vc.ReceivePlayerDataDelegate = self
    if (senderButton === selectButton_CEO) {
        vc.currentPosition = "CEO"
    } else if (senderButton === selectButton_CFO) {
        vc.currentPosition = "CFO"
    } // and so on
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM