-1

I use the following in my AppDelegate to pop an alert to user when push is received:

let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert)
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed Yes, please button")
    topWindow.isHidden = true
    topWindow.rootViewController?.openViewControllerBasedOnIdentifier("consumerProfile")
})
let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed No, thanks button")
    topWindow.isHidden = true
})
alert.addAction(yesButton)
alert.addAction(noButton)

topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

My problem is in:

topWindow.rootViewController?.openViewControllerBasedOnIdentifier("consumerProfile")

The function openViewControllerBasedOnIdentifier only exists in views based on a specific UIViewController class named BaseViewController so i get the following error:

Value of type 'UIViewController' has no member 'openViewControllerBasedOnIdentifier'

pacification
  • 5,838
  • 4
  • 29
  • 51
Johny D Good
  • 427
  • 1
  • 8
  • 26
  • what is your problem? You are not able to present Alert from AppDelegate or the error that you have posted? – Gagan_iOS Sep 04 '17 at 14:42
  • 1
    You just set your `rootViewController` to an empty `UIViewController`. How do you expect to be able to call a method of your custom `BaseViewController` on this `UIViewController`? – rmaddy Sep 04 '17 at 14:49
  • Have you seen this: https://stackoverflow.com/questions/30592521/opening-view-controller-from-app-delegate-using-swift – Avi Levin Sep 04 '17 at 14:54

1 Answers1

0

Just put your custom class as rootViewController

let topWindow = UIWindow(frame: UIScreen.main.bounds)
let topViewController = BaseViewController()
topWindow.rootViewController = topViewController
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert)
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed Yes, please button")
    topWindow.isHidden = true
    topViewController.openViewControllerBasedOnIdentifier("consumerProfile")
})
let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed No, thanks button")
    topWindow.isHidden = true
})
alert.addAction(yesButton)
alert.addAction(noButton)

topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

UPDATE

Because your ViewController doesn't have init() try this code:

let topWindow = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Name of storyboard should be same as your
let topViewController = storyboard.instantiateViewController(withIdentifier: identifier) as? BaseViewController // be sure that `identifier` of BaseViewController is same as in storyboard 
topWindow.rootViewController = topViewController
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: "message", message: "message", preferredStyle: .alert)
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed Yes, please button")
    topWindow.isHidden = true
    topViewController.openViewControllerBasedOnIdentifier("consumerProfile")
})
let noButton = UIAlertAction(title: "CANCEL", style: .default, handler: {(_ action: UIAlertAction) -> Void in
    print("you pressed No, thanks button")
    topWindow.isHidden = true
})
alert.addAction(yesButton)
alert.addAction(noButton)

topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })

UPDATE 2

There is your identifier = storyboard ID

enter image description here

Yerken
  • 299
  • 1
  • 12
  • 1
    How does this change the `rootViewController`? – rmaddy Sep 04 '17 at 14:58
  • @maddy Forgot to change topWindow.rootViewController = topViewController – Yerken Sep 04 '17 at 15:00
  • @YerkebulanAbildin can't instantiateViewController, need to use the openViewControllerBasedOnIdentifier method if i want the menu structure. When I tried the code on top the app crashed with "fatal error: unexpectedly found nil while unwrapping an Optional value" – Johny D Good Sep 04 '17 at 15:15
  • It's becouse you didn't implement init() and your BaseViewController didn't load all @IBOutlets from storyboard. Try code which I provide to you on bottom of answer – Yerken Sep 04 '17 at 15:18
  • @YerkebulanAbildin Hi. tried to implement the update but baseViewController has no identifier. Its not connected to a specific UIView but to a slider menu – Johny D Good Sep 04 '17 at 16:01
  • identifier you should put inside your storyboard for your viewController – Yerken Sep 04 '17 at 16:03
  • @YerkebulanAbildin i use identifiers. Each view has a different identifier but none of the views have this class as Custom Class and Storyboard id. Instead each UIViewController that needs access the the functionality of baseViewController is declared thus: class ContactViewController: BaseViewController but with its own custom class and Identity – Johny D Good Sep 04 '17 at 16:30
  • @YerkebulanAbildin my app is based on AKSwiftSlideMenu https://github.com/ashishkakkad8/AKSwiftSlideMenu – Johny D Good Sep 04 '17 at 16:31
  • @JohnyDGood ok, I got it. What is your initial Viewcontroller superclass? And what is class of you initial Viewcontroller – Yerken Sep 04 '17 at 17:20