我正在尝试在swift中创建警报控制器类
<!-- Your Example Markup Here -->
<!-- Place your jQuery reference here -->
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$(function(){
$('#templates-list').find("div.tmpl-select-panel[data-index='0']").css('color','red');
$('#templates-list').find("div.tmpl-select-panel[data-index='1']").css('color','blue');
});
</script>
上面的类用于显示提供消息的警报和 带有单个按钮的标题为 -
//AppDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame:UIScreen.mainScreen().bounds)
let loginVC = ViewControllerForLogin (nibName:"ViewControllerForLogin", bundle:nil)
navigationObject = UINavigationController(rootViewController: loginVC)
window?.rootViewController = loginVC
window?.makeKeyAndVisible()
return true
}
//SPSwiftAlert.swift
class SPSwiftAlert: UIViewController {
//#MARK: - Members
internal var defaultTextForNormalAlertButton = "OK"
static let sharedObject = SPSwiftAlert()
//#MARK: Functions
func showNormalAlert(controller: UIViewController, title: String, message: String) {
// create the alert
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
// add an action (button)
alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil))
// show the alert
controller.presentViewController(alert, animated: true, completion: nil)
}
}
但这给我的运行时错误
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
我该如何解决这个问题?
答案 0 :(得分:2)
所以,当我看到你的代码时,我不明白你放入window.rootViewController的部分你的loginVC而不是导航..
window?.rootViewController = navigationObject
然后,当您拨打警报时,您似乎不在窗口的视图层次结构中。
尝试将此调用写入viewDidAppear:
方法。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
dispatch_async(dispatch_get_main_queue()) {
SPSwiftAlert.sharedObject.showNormalAlert(self, title: "Invalid input", message: "Entered email address is not valid")
}
}
注意:当我们尝试显示(显示/推送)视图时,通常会发生这种情况 调节器 在另一个视图控制器上,但是演示者视图控制器是 当前不是活动视图控制器(表示演示者视图 控制器视图必须是屏幕上的顶视图)
答案 1 :(得分:0)
//添加动作(按钮) alert.addAction(UIAlertAction(title:defaultTextForNormalAlertButton,style:UIAlertActionStyle.Default,handler:nil))
//添加视图 //将self.view子视图添加到controller.view
//显示提醒 self.presentViewController(alert,animated:true,completion:nil)
答案 2 :(得分:0)
我猜你的ViewControllerForLogin已经出现,或者它在故事板中没有segue。