问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/32092289/uialertcontroller-visible-only-when-viewdidappear-has-finished-its-call