UIAlertView无法在Swift中工作

问题描述:

当我在swift中使用这段代码时,我不知道为什么应用程序终止在“alertView.show()”部分显示一个断点,有人请帮助我。UIAlertView无法在Swift中工作

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

在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,好吗? –

使用方式如下:

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

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) 

对我来说,最好的办法是...

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