1

I am trying to show a UIAlertController as my view loads. I know that's impossible during viewDidLoad() or viewWillAppear() because the view is not in the hierarchy at during the execution of those functions. However, if the view is added before viewDidAppear(), then it should be possible to show my UIAlertController during the call of that function.

Therefore, I tried this:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var AlertShow = false
    print(Configuration)

    if !AlreadyLoad {

        if Configuration.count == 0 {
            AlertShow = true
            print("First lunch")
            let Alert = UIAlertController(title: "Premier lancement", message: "C'est la première fois que l'application est lancée. Cette dernière va télécharger tous les articles.\nVeuillez patienter.", preferredStyle: .Alert)
            let OkAction = UIAlertAction(title: "Ok", style: .Default) { (action) in }
            Alert.addAction(OkAction)
            self.presentViewController(Alert, animated: true, completion: {
                AlertShow = false
            })
        }
}

But my alert is only visible after the execution of viewDidAppear(). I would like show this alert at the beginning.

I am using Swift 2, Xcode 7, and the iOS 9 SDK.

aaplmath
  • 1,717
  • 1
  • 14
  • 27
Logan Gallois
  • 594
  • 5
  • 27
  • Did you try with dispatch_after ? "But my Alert is only **visible after the execution of view viewDidAppear()**" I don't understand this clearly. – tuledev Aug 19 '15 at 10:00
  • Are you using a navigation controller or similar? If then, you can make the alert to appear on it, instead of on your current view controller. – Pablo A. Aug 19 '15 at 10:29
  • I am using Tab Bar Controller, which function is used to appear my TabBarController ? i'll try – Logan Gallois Aug 19 '15 at 13:13
  • anhtu, i test some code, and i got no error message. My alert is here, but appear when the function viewDidAppear is ended. I test print("something") after presentviewcontroller, i got the printed message and THEN i got my alert (So, at the end of viewDidAppear) – Logan Gallois Aug 20 '15 at 21:27

2 Answers2

0

The way to present UIAlertController that is independent to any view controller is to create the new UIWindow and rootViewController and put on top of the current UIWindow. This method is recommended here. How to present UIAlertController when not in a view controller?

If you want to see the implementation. You could take a look here. It simple but works very well. I converted the idea to objective c and use this in my app as well. Here is the swift version by Dylan Bettermann: https://github.com/dbettermann/DBAlertController

Community
  • 1
  • 1
thanyaj
  • 302
  • 1
  • 6
0

I tried DBAlertController method, but an other bug appear, i did this :

var window: UIWindow?

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

    //Notification demand
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
    File.changeCurrentDirectoryPath(Path[0])
    CreateFile.changeCurrentDirectoryPath(Path[0])
    UIApplication.sharedApplication().canOpenURL(NSURL(fileURLWithPath: "fbauth://authorize"))
    //deleteAllFile()
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = UINavigationController(rootViewController: ViewController2())

    window!.makeKeyAndVisible()
    return true
}

And in viewController2() i do this :

@IBOutlet var OptionButton: UIBarButtonItem!
override func viewDidLoad() {
    super.viewDidLoad()

    ReadAllFile()
    let alertController = DBAlertController(title: "DBAlertController", message: "Hello World!", preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    alertController.show(animated: true, completion: nil)
    OptionButton.title = "\u{2699}" }

but my OptionButton is nil (i linked it) and i got Fatal Error.

Logan Gallois
  • 594
  • 5
  • 27