问题
I want to present an alert when my app launches for the first time that day. I think the place to do this is the appDelegate (correct me if i am wrong). I have two problems, one: i don't know which of the functions in the appDelegate the function should reside under (have currently chosen just func application), and second: i don't know how to present the alertController to the view that is open. Thus far i have done this
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if checkIfNewDay() {
let alert = UIAlertController("some title": alertTitle, message: "some message", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Cancel, handler: nil))
// What to do here????
}
What code should i replace the comment with?
回答1:
Try to use this methode : applicationDidBecomeActive
func applicationDidBecomeActive(application: UIApplication) {
//This method is called when the rootViewController is set and the view.
if checkIfNewDay() {
let alert = UIAlertController("some title": alertTitle, message: "some message", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Okay", style: .Cancel, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
}
来源:https://stackoverflow.com/questions/35672879/how-to-display-an-alert-when-app-launches