这个问题类似于“如何从不同的viewController传递数据”,但是在这种情况下,我需要其他信息,我知道如何将数据从一个viewController传递给另一个viewController,但是我不知道如何告诉第二个viewController一个特定的信息。第一个视图中对象的状态。就我而言,在第一个视图中有一些按钮可以进入。
button.isHidden = true
Run Code Online (Sandbox Code Playgroud)
点击之后,我想告诉第二个viewController哪些按钮被点击了,也许我要创建一个代表按钮状态的变量,但是我不知道该怎么做,有人可以帮助我吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToLast" {
guard let vc = segue.destination as? FinalClass else { return }
let guest = segue.destination as! FinalClass
if let user = sender as? User {
}
}
}
Run Code Online (Sandbox Code Playgroud)
用户可以像
struct User {
var button1 = Button1 tapped
var button2 = Button2 tapped
ecc.
}
Run Code Online (Sandbox Code Playgroud)
假设您有两个viewControllers- FirstViewController和SecondViewController。
所以首先,
让声明两个数组中FirstViewController,并SecondViewController像-
var tappedButtonsArray = NSMutableArray() // In 'FirstViewController'
AND
var previousViewTappedButtonsArray = NSMutableArray() // In 'SecondViewController'
Run Code Online (Sandbox Code Playgroud)
这是我要添加的代码| 删除点击的按钮 从tappedButtonsArray在FirstViewController-
@IBAction func btnTapped(_ sender: UIButton) { // Single action for all buttons
if (tappedButtonsArray.contains(sender)){
tappedButtonsArray.remove(sender) // Remove previous tapped button on unselect
}
else{
tappedButtonsArray.add(sender) // Add tapped button on select
}
}
Run Code Online (Sandbox Code Playgroud)
而这是你的执行SEGUE方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToLast" {
guard let secondViewController = segue.destination as? SecondViewController else { return }
secondViewController.previousViewTappedButtonsArray = tappedButtonsArray
}
}
Run Code Online (Sandbox Code Playgroud)
我已经完成了没有任何模型的简单数组概念,因此您可以理解以下内容。希望对您有所帮助。