我正在try 使用输入生成一条警告消息,然后从输入中获取值.我发现了很多如何制作输入文本字段的很好的教程.但我无法从alert 中获取价值.

推荐答案

针对SWIFT 3及更高版本更新:

//1. Create the alert controller.
let alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .alert)

//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
    textField.text = "Some default text"
}

// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
    let textField = alert.textFields![0] // Force unwrapping because we know it exists.
    print("Text field: \(textField.text)")
}))

// 4. Present the alert.
self.present(alert, animated: true, completion: nil)

Swift 2.十、

假设你想在iOS上发布行动alert :

//1. Create the alert controller.            
var alert = UIAlertController(title: "Some Title", message: "Enter a text", preferredStyle: .Alert)

//2. Add the text field. You can configure it however you need.
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
    textField.text = "Some default text."
})

//3. Grab the value from the text field, and print it when the user clicks OK. 
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { [weak alert] (action) -> Void in
    let textField = alert.textFields![0] as UITextField
    println("Text field: \(textField.text)")
}))

// 4. Present the alert.
self.presentViewController(alert, animated: true, completion: nil)

Ios相关问答推荐

更新Flutter项目时出错:CocoaPods找不到pod webview_flutter_wkwebview的兼容版本""

SwiftUI中ForEach和@ State数组的可能并发问题

如何在SwiftUI ScrollView中zoom 中心项目?

在iOS中禁用URLSession自动重试机制

阿拉伯文变音符号(Harakat)在文本对齐的UILabel中未对齐

IOS:在UI线程中执行代码的闭包样式的替代

当文本为空时,具有垂直轴的SwiftUI文本字段不会与FirstTextBaseline对齐

在 SwiftUI 中以编程方式插入内嵌图像

iOS设备网页自动化中,在向字段发送文本后未启用按钮

如何在 SwiftUI 中同时使用 LongPressGesture 和 ScrollView 中的滚动?

UIControl 子类 - 按下时显示 UIMenu

在 SwiftUI 中使用 .rotation3DEffect 在视图之间翻转

如何为视图的 colored颜色 和宽度设置动画?

在 iPad 上删除列表的顶部和底部行

DateFormatter 使用 TimeZone 和 Locale 的特定组合返回 nil

无法将NSTaggedPointerString类型的值转换为NSNumber

为信用卡输入格式化 UITextField,例如 (xxxx xxxx xxxx xxxx)

NSPredicate 用于测试 NULL 和空白字符串

iOS 7 圆形框架按钮

使用 xib 创建可重用的 UIView(并从情节提要中加载)