0

if i have a ViewController, which has an instance ShowAlert. showAlert will show an alert controller, but is there any way the instance showAlert can know about who instantiated it? i.e. is there any way not to pass self to ShowAlert's instance variable viewController and instead infer it by parent or something similar? should i somehow be delegating this, instead?

class ViewController: UIViewController {
    var showAlert = ShowAlert()

    override viewDidLoad() {
        super.viewDidLoad()
        showAlert.viewController = self
        showAlert.now()
    }
}

class ShowAlert: NSObject {

    var viewController: UIViewController!

    func now() {
        let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
        viewController.showViewController(alert, sender: self)
    }

}

thanks -

1

1 Answer 1

1

sure instead of:

func now() {

do:

func now(parentVC : UIViewController ){
  let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    parentVC.showViewController(alert, sender: self)
}

and you can call it like:

showAlert.now(self)
Sign up to request clarification or add additional context in comments.

Comments

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.