简体   繁体   English

取消模态视图时VC中的触发功能

[英]Trigger function in VC when modal view is dismissed

I'm trying to trigger a function after dismissing a modal VC (FirstStartVC) back to the main VC. 我试图在将模态VC(FirstStartVC)解除回主VC后触发一个函数。 I know that I have to use delegation but it doesn't work and my debug area stays empty. 我知道我必须使用委托,但它不起作用,我的调试区域保持空白。

In other question topics there were people who had it work exact the same way like below. 在其他问题主题中,有人将其工作完全相同,如下所示。 So I have no idea what I'm doing wrong. 所以我不知道我做错了什么。 Does anyone know what I need to change to the code? 有谁知道我需要更改代码?

//  FirstStartVC.swift
//

import UIKit
import CoreData
import JSSAlertView

protocol NewUser: class {
    func newUserAction()
}

class FirstStartVC: UITableViewController, UITextFieldDelegate {

    var delegation : NewUser?

    func saveNewUser(){
            self.delegation?.newUserAction()
            self.dismiss(animated: true, completion: nil)
        }
    }

    @IBAction func saveSettings(_ sender: Any) {
        self.saveNewUser()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print (delegation)

    }
}





//
//  ViewController.swift
//

import UIKit
import UserNotifications
import GoogleMobileAds
import CoreData
import JSSAlertView

class ViewController: UIViewController, UNUserNotificationCenterDelegate, NewUser {
    func newUserAction() {
        print("Reload some labels")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        var firstStart = FirstStartVC()
        firstStart.delegation = self

    }
}

Swift 3 斯威夫特3

In your main VC viewDidLoad add: 在你的主VC viewDidLoad添加:

NotificationCenter.default.addObserver(self, selector: #selector(mainVc.functionName), name:"NotificationID", object: nil)

and add a function in main VC 并在主VC中添加一个功能

func functionName() {

    // Do stuff

}

in FirstStartVC call the method with 在FirstStartVC中调用方法

NotificationCenter.default.postNotificationName("NotificationID", object: nil)

Hope this helps! 希望这可以帮助!


A simple edit on Swift 4 Swift 4上的简单编辑

NotificationCenter.default.addObserver(self, selector: #selector(self.funcName), name: NSNotification.Name(rawValue: "NotificationID"), object: nil)

Put @objc before the function definition. @objc放在函数定义之前。

@objc func functionName() {

    // Do stuff

}

In your code, you have: 在您的代码中,您有:

func saveNewUser(){
        self.delegation?.newUserAction()
        self.dismiss(animated: true, completion: nil)
    }
}

Simply write the code you want to run after dismissing in completion: : 只需在completion:解雇后编写要运行的代码:

func saveNewUser() {
        self.delegation?.newUserAction()
        self.dismiss(animated: true, completion: { finished in
            // on completion
        })
    }
}

(You might not even need to say finished in or anything like that.) (你可能甚至不需要finished in或类似的东西。)

If the code you need to execute within newUserAction() is a part of FirstStartVC , you should just call it inside the completion handler of the dismiss(_:animated:) method. 如果您需要内执行代码newUserAction()是的一部分FirstStartVC ,你应该只把它称为完成处理程序内dismiss(_:animated:)方法。 However, if you need the code to execute on the VC that presented FirstStartVC , make sure that it conforms to the NewUser protocol. 但是,如果您需要在提供FirstStartVC的VC上执行代码,请确保它符合NewUser协议。 You could do something like this (assuming the presenting VC was named something like PresentingViewController - change it to whatever is the case for your project): 你可以做这样的事情(假设呈现的VC被命名为类似于PresentingViewController - 将其更改为你的项目的情况):

class PresentingViewController: UIViewController {

    // However you instantiate the FirstStartVC
    let firstStart = FirstStartVC()

    // set the delegation property to self
    firstStart.delegation = self

}

Then at the bottom of the screen create an extension so it conforms to the protocol : 然后在屏幕底部创建一个extension ,使其符合protocol

extension PresentingViewController: NewUser {

    func newUserAction() {

        // Here you can do whatever you want when the delegation calls this method

    }

}

EDIT: - Further recommendation... 编辑: - 进一步的建议......

I always find it best practice with delegates to use a weak reference to prevent memory problems. 我总是发现代表们最好的做法是使用weak引用来防止内存问题。 To do so you have to make sure to set the protocol as :class , which you've already completed: protocol NewUser: class . 为此,您必须确保将protocol设置为:class ,您已经完成了: protocol NewUser: class So then when you create the property at the top of FirstStartVC you would just say 那么当你在FirstStartVC的顶部创建属性时,你会说

weak var delegation: NewUser?

Your code will still run the same, I just recommend doing it this way as it's helped me avoid memory issues in numerous instances. 你的代码仍然会运行相同,我只是建议这样做,因为它帮助我在许多实例中避免了内存问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM