[英]How can I call a function on a MasterVC, executed by the dismissal of a pushed VC?
I have a MasterVC with a UIImageView
with an alpha of 0, which switches alpha to 1 on the press of a UIButton
. 我有一个MasterVC,其
UIImageView
的alpha值为0,在按下UIButton
将alpha切换为1。 The same button press pushes to a SecondaryVC displaying an image over the MasterVC with "Over Current Context". 按下相同的按钮将推动SecondaryVC在MasterVC上以“ Over Current Context”显示图像。
If the SecondaryVC is dismissed, I want to set the alpha of the MasterVC's UIImageView
back to 0, but am having difficulty finding how to call a function using only a dismiss to return to the MasterVC. 如果关闭了SecondaryVC,我想将MasterVC的
UIImageView
的alpha设置回0,但是很难找到如何仅使用dismiss返回MasterVC来调用函数的方法。
Here is my button to set the alpha and push to the SecondaryVC. 这是我的按钮,用于设置Alpha并推送至SecondaryVC。 Using a segue back seems out of the question since I've heard it creates memory issues.
因为我听说过使用segue会造成内存问题,所以似乎无法使用它。
I tried using a protocol, but it wasn't playing well with CGFloat. 我尝试使用协议,但与CGFloat配合使用效果不佳。
@IBOutlet weak var background: UIImageView!
@IBOutlet weak var backgroundTable: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
background.image = lightBG
backgroundTable.image = tableBG
backgroundTable.alpha = 0
}
@IBAction func unknownProperties(_ sender: UIButton) {
UIView.animate(withDuration: 1.0, animations:{
self.backgroundTable.alpha = 1
})
getOverlay()
}
Use of NotificationCenter
could be a solution here . 使用
NotificationCenter
可能是此处的解决方案。
Step 1 : Add an observer at viewDidLoad
步骤1:在
viewDidLoad
处添加观察者
NotificationCenter.default.addObserver(
self,
selector: #selector(self.functionToExecute),
name: NSNotification.Name.init("UpdateBackgroundTableAlpha"),
object: nil
)
Step 2 : Post a notification just before the SecondaryVC is dismissed . 步骤2:在关闭SecondaryVC之前发布通知。
NotificationCenter.default.post(name: NSNotification.Name.init("UpdateBackgroundTableAlpha"), object: nil)
Step 3 : Define functionToExecute
in MasterVC class scope . 步骤3:在MasterVC类范围内定义
functionToExecute
。
func functionToExecute(){
print("Make BackgroundTable Great Again!")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.