我有一个Swift文件,其中包含四个独立的元组数组。在ViewControllerA中,我有四个不同的按钮,每个按钮都会更改“ selectedPosition”的值,并设置为触发“ SegueAtoB”。然后应在ViewControllerB中使用“ selectedPosition”来选择适当的数组,以在ViewControllerB上填充UIPickerView。
当前,我的“准备segue”方法仅在第二次按下按钮时才传递正确的数据,例如当应用程序加载到ViewControllerA中时,我按下一个按钮,它将带我到ViewControllerB,但是为UIPickerView选择的数组是默认的,当我退回到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:
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
}
}
}
我需要将UIPickerView更新为从ViewControllerA到ViewControllerB的第一个序列中的相关数组,而不是在第一个序列中使用默认值。
答案 0 :(得分:0)
如果多个控件导致相同的结果,则需要检查sender
函数中的prepare(for:)
。如果它是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
}