我试图在用户首次下载应用时弹出一次UIAlertController。但是,当我将代码放入didFinishLaunchingWithOptions时,我收到以下错误,
2015-01-15 23:54:45.306 WeddingApp Warning: Attempt to present UIAlertController: on UITabBarController: whose view is not in the window hierarchy!
我的代码如下:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if window == nil {
println("window is nil");
return true;
}
if window!.rootViewController == nil {
println("window!.rootViewController is nil");
return true;
}
//TabBarController//
let tabBarController: UITabBarController = window!.rootViewController! as UITabBarController;
UITabBar.appearance().tintColor = UIColor.magentaColor();
UITabBar.appearance().translucent = false;
/* tableView cells are now completely visable. They do not hie behind the tabBar */
tabBarController.viewControllers = [
TwelveToTenMonths(nibName: nil, bundle:nil),
NineToSevenMonths(nibName: nil, bundle:nil),
SixToFourMonths(nibName: nil, bundle:nil),
ThreeToOneMonth(nibName: nil, bundle:nil),
];
var alert = UIAlertController(
title: "Welcome to WeddingApp!",
message: "Thank You for choosing the \nWedding App for your organization needs. \n\nFor more great tools please visit our website www.wedme.com",
preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Default, handler: nil));
self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil);
return true
}
***注意:我也尝试将以下内容添加到appDelegate而不是上面的代码,但每当我返回应用程序时警报会继续出现.....
func applicationDidBecomeActive(application: UIApplication) { //Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
showAlert();
}
func showAlert(){
println("alert");
var alert = UIAlertController(
title: "Welcome to WeddingApp!",
message: "Thank You for choosing the \nWedding App for your organization needs. \n\nFor more great tools please visit our website www.wedme.com",
preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Default, handler: nil));
self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil);
}
有没有人知道解决这个问题?
答案 0 :(得分:0)
在第一种情况下,您从未添加过UIAlertController(您可以尝试将其添加为子视图控制器)。
https://stackoverflow.com/a/17012439/3324388
在第二种情况下,您将根控制器分配给UIAlertController,并且从不将根控制器分配回UITabBarController。你需要重新设置它。
您是否尝试过使用UIAlertView?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html
答案 1 :(得分:0)
尝试这样的事情:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
if self.window!.rootViewController as? UITabBarController != nil {
var tababarController = self.window!.rootViewController as UITabBarController
.
.
.
let alertController = UIAlertController(title: "Welcome...", message: "Thank You...", preferredStyle: .Alert)
tababarController.presentViewController(alert, animated: true, completion: nil);
}
return true
}