2014-06-06 45 views
3

當我在swift中使用這段代碼時,我不知道爲什麼應用程序終止在「alertView.show()」部分顯示一個斷點,有人請幫助我。UIAlertView無法在Swift中工作

var alertView = UIAlertView(
    title: "Hey", 
    message: "Hello", 
    delegate: self, 
    cancelButtonTitle: "Cancel" 
) 
alertView.show() 

回答

17

在Xcode 6.0 UIAlertView中類:

UIAlertView中已被棄用。相反,使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle 。

在SWIFT(iOS版8和OS X 10.10),你可以這樣做:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel)) 
     alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in 
       println("User click Ok button") 
      })) 
     self.presentViewController(alert, animated: true, completion: nil) 

func handleCancel(alertView: UIAlertAction!) 
     { 
      println("User click cancel button") 
     } 

如果你想在 'ActionSheet' 使用,而不是 '警報',你只需要改變UIAlertControllerStyle爲例如:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet) 
+0

請問您可以告訴我如何爲其添加TextField? –

+1

@ AppleKid,加alert.addTextFieldWithConfigurationHandler(configurationTextField)和函數:FUNC configurationTextField(文本框:的UITextField) { 的println( 「configurat僱用文本字段」) 如果讓tField = {文本框 self.textField =文本域! self.textField.text =「Hello world」 } } 現在,您可以在handleCancel或handleOk閉包上打印用戶輸入:println(self.textField.text) –

+0

@AppleKid,好嗎? –

-1

使用方式如下:

var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert) 
altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil)) 
self.presentViewController(altMessage, animated: true, completion: nil) 
+0

使用「func showInView(_ view:UIView!)」在這裏添加textField是ref。 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html#//apple_ref/occ/instm/UIActionSheet/showInView: –

+0

我沒有得到它..可以請你給我看一個小代碼。 –

+0

這不適用於iOS 7. – Sulthan

1

UIAlertView中被棄用的iOS 8,但是斯威夫特支持iOS7,你不能用在iOS 7 UIAlertController添加下面的方法來解決這個問題:

func showAlert(title:NSString, message:NSString,owner:UIViewController) { 
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") { 
     var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
     owner.presentViewController(alert, animated: true, completion: nil) 
    } 
    else { 
     let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK") 
     alertView.alertViewStyle = .Default 
     alertView.show() 
    } 
} 

,並從這樣的代碼的任何地方調用方法:

showAlert(APP_NAME,message: "Add your alert message here" ,owner: self) 
0

對我來說,最好的辦法是...

class ViewController: UIViewController, UIAlertViewDelegate { 
var allarme = UIAlertView(title: "Warning", message: "This is a best way to create a alarm message", delegate: self, cancelButtonTitle: "OK") 
     allarme.show() 

記得要導入的類UIAlertViewDelegate