我试图在我的快速应用程序中显示UIAlertView
alert = UIAlertView(title: "",
message: "bla",
delegate: self,
cancelButtonTitle: "OK")
alert!.show()
=> BAD_ACESS错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()
所以我怀疑自己。我把它设置为零
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
=> ARM_DA_ALIGN错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()
完整的代码
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {
@lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
@lazy var locationManager = CLLocationManager()
var alert: UIAlertView? = nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//setup dummy window
self.window.backgroundColor = UIColor.whiteColor()
self.window.rootViewController = UIViewController()
self.window.makeKeyAndVisible()
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
return true
}
}
怎么做对了? :)
你应该这样做:
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
即使UIAlertView
在iOS 8中被弃用,你也可以使用它而不是通过它的init函数。例如:
var alert = UIAlertView()
alert.title = "Title"
alert.message = "message"
alert.show()
至少这是迄今为止我唯一能够成功使用UIAlertView
的方式。我不确定这是多么安全。
这就是我用来在swift中制作警报弹出窗口的方法。
let myAlert = UIAlertView(title: "Invalid Login",
message: "Please enter valid user name",
delegate: nil, cancelButtonTitle: "Try Again")
myAlert.show()
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
处理行动:
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: {
action in switch action.style {
case .Default:
println("default")
case .Cancel:
println("cancel")
case .Destructive:
println("destructive")
}
}))
我成功地使用此代码显示了UIAlertView:
var alert = UIAlertView()
alert.title = "Title"
alert.message = "Message"
alert.addButtonWithTitle("Understood")
alert.show()
代码“alert.addButtonWithTitle(”Understood“)”为用户在读取错误消息后添加了一个按钮。
希望这对你有所帮助。
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)