0

I'd like to get access to a ViewController from an extension and then override a function from a library which should change variables or invoke methods from a specific ViewController (in this case "ViewController").

How can I do that? Or is there a more recommended option?

(Hint: I don't want to instantiate a new VC)

import PopupDialog

class ViewController: UIViewController {

    //e.g. variable and method to access from extension

    var score: Int = 0;

    func startGame(){
        ...
    }
}
extension PopupDialog{
    open override func viewWillDisappear(_ animated: Bool) {
        //change variables
        //maybe also invoke methods
    }
}
8
  • How does PopupDialog is using in this code? Is it linked to ViewController somehow? Commented Aug 2, 2018 at 9:55
  • i don't think you can override methods in extension. Try subclassing instead Commented Aug 2, 2018 at 9:55
  • @AndrewBogaevskyi the PopupDialog is imported in ViewController and its used there Commented Aug 2, 2018 at 9:58
  • 1
    @LalKrishna it works. I tested it with print("xy") in the console. So I don't want to change something from PopupDialog, I just want to change statements from my ViewController when the function viewWillDisappear() from PopupDialog gets invoked Commented Aug 2, 2018 at 10:00
  • what variable you need to change? Do you want to change ViewController.score from PopupDialog's viewWillDisappear method? Commented Aug 2, 2018 at 10:06

1 Answer 1

1

You can use Notification or Protocol to communicate between ViewControllers

Notification Example:

Create an Extension to use Notification names:

extension Notification.Name {
    static let ScoreUpdateNotification = Notification.Name("ScoreUpdateNotification")
}

Add Observer method in First ViewController's DidLoad

NotificationCenter.default.addObserver(self, selector: #selector(triggerThisMethod), name: Notification.Name.ScoreUpdateNotification, object: nil)

@objc func triggerThisMethod(_ notification: NSNotification) {
        // When the notification arrives, this method will be called
        if let object = notification.object {
            // If there is Object passed with notification you will get it here.
        }
    }

To Post Notification

let objectToPass = 23
NotificationCenter.default.post(name: .ScoreUpdateNotification, object: objectToPass)

Protocol

Create Protocol and a weak variable in ViewController B

protocol ProtocolB: class {
    func didUpdateScore(_ score: Int)
}

weak var delegate: ProtocolB?

When you Present/Push ViewController B from A

let viewControllerB = // ...
viewControllerB.delegate = self
//present viewControllerB

Add Protocol Methods to ViewController A

extension ViewController: ProtocolA {
    func didUpdateScore(_ score: Int) {
        // Do things
    }
}

To trigger methods call:

delegate?.didUpdateScore(25)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! I'll try both ways, I think I could get in trouble because I only have one ViewController class file and from there I just invoke a new one modally. So I have no file for ViewController B (I'll accept your answer later, when everything is fine)
PopupDialog ? What's this about? Is it a viewcontroller? NB: You don't have to use both way. Use the convenient one. Normally notifications are used when you Trigger multiple observers.
Yes it is! Worked fine with the notification pattern, but I'm posting the notification in my viewWillDisappear() from the PopUpDialog extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.