简体   繁体   English

如何通过关闭推送的VC来在MasterVC上调用函数?

[英]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.

相关问题 另一个ios应用程序推送通知后,如何在ios应用程序中调用功能? - How can I call a function in my ios app once another ios app pushed a notification? 如何检查我的NavigationController子类是否已推送和弹出VC? - How can I check if my NavigationController subclass has had VC's pushed & popped? 弹出窗口关闭后,如何在transitionCoordinator中安排新的演示文稿 - How can I schedule a new presentation in transitionCoordinator after popover dismissal 如何检测到新的VC被MoreController推送? - How to detect that a new VC is pushed by the MoreController? 如何在第二个VC中使用第一个VC中的方法? - How can I use methods from first VC in second VC? 我如何才能推送两个VC但只能呈现顶级VC - How can I push two VC but only presenting top VC Swift:在解雇的完成处理程序中呈现 VC - Swift: Presenting VC in completion handler of dismissal 如何从在情节提要中定义了segue的推送的vc执行segue - How to perform a segue from a pushed vc that has the segue defined in the storyboard 第一次推送vc时,如何弹出vc 12次,如何弹出? - how to pop when first pushed vc then presented modal vc 12 times? 如何从左到右滑动禁用ViewController解雇? - How do I disable ViewController dismissal on left-to-right swipe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM