LoginSignup
15
14

More than 5 years have passed since last update.

AppDelegateでAlertDialogを出したい場合(rootViewControllerの取得)

Last updated at Posted at 2015-02-18

rootViewControllerの取得

AppDelegateでAlertDialogを出すのには、rootViewControllerを取得しないとそもそも表示できないので

AppDelegate
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

とやっといて

AppDelegate
self.window?.rootViewController?

で取得する。

debugとかでalertdialogを出したい場合

debugモードを使う方法は、Swift での #ifdef DEBUG のやり方を参考にしてください。

AppDelegate
#if DEBUG
    var body:NSString = "えらーめっせーじ" + "\(error)"
    var alertController:UIAlertController = UIAlertController(title: "通知", message: body, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
#endif
15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14