在解除模式控制器时更改变量



EDIT:我决定改变我的应用程序的工作方式,所以这个问题得到了解决。感谢所有提供帮助的人!

我有一个模式控制器,当我按下按钮时,它会关闭视图。我想做的是,当我忽略另一个视图控制器中的变量时,更改它,这可能吗?或者,如果这不起作用,我有办法访问另一个swift文件的更改变量吗?我将在下面添加我的代码:

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
}

} 

ps:-我建议你阅读https://docs.swift.org/swift-book/LanguageGuide/Protocols.html&苹果文档

最新更新