1
votes

I am trying to create the alert controller class in swift

//AppDelegate.swift:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow(frame:UIScreen.mainScreen().bounds)

        let loginVC = ViewControllerForLogin (nibName:"ViewControllerForLogin", bundle:nil)

        navigationObject = UINavigationController(rootViewController: loginVC)

        window?.rootViewController = loginVC

        window?.makeKeyAndVisible()
        return true
    }

//SPSwiftAlert.swift

class SPSwiftAlert: UIViewController {

    //#MARK: - Members

    internal var defaultTextForNormalAlertButton = "OK"

    static let sharedObject = SPSwiftAlert()

    //#MARK: Functions

    func showNormalAlert(controller: UIViewController, title: String, message: String) {

        // create the alert
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil))

        // show the alert
        controller.presentViewController(alert, animated: true, completion: nil)

    }    
}

the above class is used display the alert with provided message and title with single button as-

 SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")

but this giving me runtime error as

Attempt to present <UIAlertController: 0x7f8c805e8e80> on <Swaft_Login_Demo.ViewControllerForLogin: 0x7f8c8042d4b0> whose view is not in the window hierarchy!

How should i resolve this ?

3
Are you trying to present view controller from already presented view controller ? SPSwiftAlert is it presented already.Hasya
No, everything is in front of you!Suhas Arvind Patil

3 Answers

2
votes

So, when I saw your code I dont understand the part where you put to window.rootViewController your loginVC instead of the navigation..

window?.rootViewController = navigationObject

Then, it seems you are not in the window's view hierarchy when you call your alert.

Try to write this call to the viewDidAppear: method.

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        dispatch_async(dispatch_get_main_queue()) {
              SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
        }
}

NOTE: This generally happens when we try to show (present/push) the view controller over another view controller but the presenter view controller is currently not active view controller (means the presenter view controller view must be top view on the screen)

0
votes
  1. First why are you not presenting the alert controller on your view controller itself rather than making a new view controller and passing self(your viewcontroller) in that for alert to be displayed.
  2. As far as the problem is concerned, you have never added the "SPSwiftAlert" view to any view.

// add an action (button) alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil))

// add view // add self.view subview to controller.view

// show the alert self.presentViewController(alert, animated: true, completion: nil)

0
votes

I guess your ViewControllerForLogin is presented already or it is not having segue in storyboard.