-2

I am setting root view controller of my app from below code

let navController = UINavigationController()
    //App Theming
    navController.navigationBar.barTintColor = Constant.AppColor.navigationColor
    navController.navigationBar.tintColor = Constant.AppColor.navigationBarTintColor
    navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: Constant.AppColor.navigationColorTextColor]
    navController.pushViewController(viewContoller, animated: true)
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = navController
    appDelegate.window?.makeKeyAndVisible()

Now after I set one controller as root. Then I show alert on the window from below code.

    let window = UIApplication.shared.keyWindow!
     window.addSubView(mainView)

But the alert does not show up. please let me know what is the issue ? I am not able to understand .

EDIT: On app launch I call the api in app background thread so I get session expire & on session expire I am showing alert to user. So by the time alert is shown & new controller if navigated which hides the alert because new controller is crated .After that I try to show alert then it does not work

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • see this once may be it helps you https://stackoverflow.com/questions/36155769/how-to-show-uialertcontroller-from-appdelegate/36156077 – Anbu.Karthik Mar 27 '18 at 06:31
  • can u please post code of alert? –  Mar 27 '18 at 06:36
  • Possible duplicate of [How to show UIAlertController from Appdelegate](https://stackoverflow.com/questions/36155769/how-to-show-uialertcontroller-from-appdelegate) – Vinaykrishnan Mar 27 '18 at 06:36
  • Check my answer: [LINK](https://stackoverflow.com/a/49506453/4061501) – Lal Krishna Mar 27 '18 at 07:12
  • Possible duplicate of [Present UIAlertController from AppDelegate](https://stackoverflow.com/questions/26952061/present-uialertcontroller-from-appdelegate) – Enea Dume Mar 27 '18 at 08:12
  • @Techiee where did you put the above code? Is your alert a `UIAlertController` ? Where do you want to show it? – trungduc Mar 30 '18 at 04:11
  • I am not using any UIAlertViewController. I am adding a custom view window & showing , hiding It with animation. – TechChain Mar 30 '18 at 12:51
  • @Techiee Please make your question clearer and someone will give you a right answer. I don't know why you need `appDelegate.window?.rootViewController = navController` when showing alert and what is `mainView`? One more, where did you put the above code? – trungduc Mar 31 '18 at 05:06

3 Answers3

0

Don't use window.addSubView(mainView) to show the alert.

Use a viewController that manages mainView and present it from the rootViewController.

See https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html on how to present a viewController

D. Mika
  • 2,577
  • 1
  • 13
  • 29
  • I have made a custom class for this so I can't get the current controller object – TechChain Mar 27 '18 at 06:42
  • You want to present an alert from a custom class? This has been discussed a few times. (e.g.https://stackoverflow.com/questions/36155769/how-to-show-uialertcontroller-from-appdelegate) – D. Mika Mar 27 '18 at 06:46
  • Why do I need to create a new UIWindow() object every time I show an alert. I can use appdelegate window for that – TechChain Mar 27 '18 at 06:48
  • Normally an alert is displayed by a view controller and you can use the present method there. But if you are in a class that is not a ViewController or has no reference to a ViewController, things are more complicated. – D. Mika Mar 27 '18 at 06:52
  • I can't change the approach of showing the alert on view controller because I have reached so far in my code – TechChain Mar 27 '18 at 08:36
  • Than check then following https://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller – D. Mika Mar 27 '18 at 08:49
0

First add root viewcontroller like this then add code for alert on viewdidappear in your view controller

let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let object  = storyboard.instantiateViewController(withIdentifier:"yourViewController") as! yourViewController            
let addNavi = UINavigationController(rootViewController: object)            

self.window?.rootViewController = addNavi self.window?.makeKeyAndVisible()

0

You can not show any alert on UIwindow, UINavigationController and on UItabbarController because these all are indicate to root.

You need to show on any UIViewcontroller object.

msanford
  • 11,803
  • 11
  • 66
  • 93