2014-06-08 20 views
15

tôi viết mã này trong nhanh chóng và Xcode 6lỗi Alert trong nhanh chóng

@IBAction func Alert(sender : UIButton) { 
    var alert : UIAlertView = UIAlertView(title: "Hey", message: "This is one Alert",  delegate: nil, cancelButtonTitle: "Working!!") 

    alert.show() 
} 

Xcode không hiển thị lỗi trong trình biên dịch.

nhưng trong Simulator APP thất bại và trả lại lỗi:

(lldb) 
thread 1 EXC_BAD_ACCESS(code 1 address=0x20) 
+0

Trông giống như một lỗi đối với tôi. – Adam

+1

tôi tìm câu trả lời trong http://stackoverflow.com/questions/24084521/uialertview-is-not-working-in-swift – rickdecard

Trả lời

36

Có một lỗi trong shim Swift của initializer tiện UIAlertView, bạn cần phải sử dụng initializer đồng bằng

let alert = UIAlertView() 
alert.title = "Hey" 
alert.message = "This is one Alert" 
alert.addButtonWithTitle("Working!!") 
alert.show() 

Mã kiểu này cảm thấy chân thực hơn với Ngôn ngữ Swift. Trình khởi tạo thuận tiện có vẻ như Mục tiêu-C'ish đối với tôi. Chỉ là ý kiến ​​của tôi.

Lưu ý: UIAlertView là phản (xem khai) nhưng Swift hỗ trợ iOS7 và bạn không thể sử dụng UIAlertController trên iOS 7

Xem Tuyên bố UIAlertView trong Xcode

// UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead 
class UIAlertView : UIView { 

Một Alert trong Swift iOS 8 Chỉ

var alert = UIAlertController(title: "Hey", message: "This is one Alert", preferredStyle: UIAlertControllerStyle.Alert) 
alert.addAction(UIAlertAction(title: "Working!!", style: UIAlertActionStyle.Default, handler: nil)) 
self.presentViewController(alert, animated: true, completion: nil) 
Các vấn đề liên quan