Reputation: 3058
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 -
Upvotes: 0
Views: 62
Reputation: 89519
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)
Upvotes: 1