UIAlertView在Swift中不起作用。

3

当我在Swift中运行此代码时,我不知道为什么应用程序会在“alertView.show()”部分显示断点并终止运行。请有经验的人帮帮我。

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

17

从Xcode 6.0开始,UIAlertView已经不被推荐使用。建议改用UIAlertController,并将preferredStyle设置为UIAlertControllerStyleAlert。

在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")
        }

如果您想在“操作表”中使用而不是“警报”,只需更改UIAlertControllerStyle,例如:

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

请问您如何向其中添加一个文本框? - Apple Kid
1
@AppleKid,添加 alert.addTextFieldWithConfigurationHandler(configurationTextField) 和以下函数:func configurationTextField(textField: UITextField!) { println("配置文本框")if let tField = textField { self.textField = textField! self.textField.text = "你好,世界" }}现在你可以在 handleCancel 或 handleOk 闭包中打印用户输入的内容:println(self.textField.text) - Guy Kahlon
尝试在闭包中使用self.textField.text,但它会显示“隐式使用闭包中的'self'; 使用'self.'来明确捕获语义”。 - User

1

UIAlertView在iOS 8中已被弃用,但Swift支持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中导入


-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)

使用 " func showInView(_ view: UIView!) " 在这里添加 textField,参考链接:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html#//apple_ref/occ/instm/UIActionSheet/showInView: - Divya Bhaloidiya
我没明白,请你能否给我展示一小段代码? - Apple Kid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接