Swift 关闭模态控制器时更改变量
编辑:我决定改变我的应用程序的工作方式,所以这个问题得到了解决。感谢所有帮助过我的人 我有一个模态控制器,当我按下一个按钮时,它会关闭视图。我想做的是在我关闭另一个视图控制器时更改它,这可能吗?或者,如果这不起作用,我是否有办法访问另一个swift文件的已更改变量?我将在下面添加我的代码:Swift 关闭模态控制器时更改变量,swift,xcode,Swift,Xcode,编辑:我决定改变我的应用程序的工作方式,所以这个问题得到了解决。感谢所有帮助过我的人 我有一个模态控制器,当我按下一个按钮时,它会关闭视图。我想做的是在我关闭另一个视图控制器时更改它,这可能吗?或者,如果这不起作用,我是否有办法访问另一个swift文件的已更改变量?我将在下面添加我的代码: class PopupViewController: UIViewController { var event = "" override func viewDidLoad
class PopupViewController: UIViewController {
var event = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func dismiss(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func event910(_ sender: Any) {
event = "storyTime"
dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ViewController
vc.event = event
}
}
我想将已更改的变量事件传递给另一个视图控制器,如何执行此操作?委托视图控制器如下所示:- 它是您将数据发送到下一个swift文件的地方
protocol myprotocol {
func anyfunction(_ param1:String)
}
struct mystruct1 {
var delegate:myprotocol?
// where you want tot start the delegate / send the data to the next file
func anymethod(){
delegate.anyfunction(sendTheDataYouWant)
}
}
// it is here you will receive the data
class anyclass:UIViewController ,myprotocol {
let class1 = mystruct1()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
class1.delegate = self
}
func anyfunction(param1:String){
// here Save the data you want
// because this function will be triggered as delegate will be called
}
}
附言:我建议你阅读
&apple docs因为我要解除模态控制器,所以没有segue,对吗?我会这样做,但解雇不是一个环节。你可以使用委托视图模式,在解雇你的模型之前可以在其中传输数据。你能解释一下我是如何做到的吗?添加了新的答案希望这对你有帮助:在Delegate.anyfunctionsendthedata中,如何从类中引用变量?另一个视图控制器与PopupViewController在视图层次结构方面的关系如何?