简体   繁体   中英

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. 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

In your main VC viewDidLoad add:

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

and add a function in main VC

func functionName() {

    // Do stuff

}

in FirstStartVC call the method with

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

Hope this helps!


A simple edit on Swift 4

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

Put @objc before the function definition.

@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: :

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.)

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. However, if you need the code to execute on the VC that presented FirstStartVC , make sure that it conforms to the NewUser protocol. 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):

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 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. To do so you have to make sure to set the protocol as :class , which you've already completed: protocol NewUser: class . So then when you create the property at the top of FirstStartVC you would just say

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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