用于获取用户数据的Xcode7 UIAlertController
我需要一些帮助,从工具栏按钮中调出自定义警报。目标是弹出警报,要求用户输入标题和说明。警报将有两个文本字段,一个用于标题,一个用于详细信息,并带有“创建”和“取消”按钮 代码如下,但我在Xcode 7.0.1中遇到了两个相同的错误: 无法调用非函数类型“[UITextField]”的值 此错误发生在以下行上:用于获取用户数据的Xcode7 UIAlertController,xcode,swift,uitextfield,xcode7,uialertcontroller,Xcode,Swift,Uitextfield,Xcode7,Uialertcontroller,我需要一些帮助,从工具栏按钮中调出自定义警报。目标是弹出警报,要求用户输入标题和说明。警报将有两个文本字段,一个用于标题,一个用于详细信息,并带有“创建”和“取消”按钮 代码如下,但我在Xcode 7.0.1中遇到了两个相同的错误: 无法调用非函数类型“[UITextField]”的值 此错误发生在以下行上: let itemTitle = alert.textFields!(0) as UITextField! let itemDetail = alert.textFiel
let itemTitle = alert.textFields!(0) as UITextField!
let itemDetail = alert.textFields!(1) as UITextField!
完整代码:
@IBAction func addNewToDoListItem(sender: UIBarButtonItem) {
var alert = UIAlertController(title: "New ToDo Item", message: "Please enter a title and details for the new ToDo list item", preferredStyle: UIAlertControllerStyle.Alert)
var createItemAction = UIAlertAction(title: "Create", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
let itemTitle = alert.textFields!(0) as UITextField!
let itemDetail = alert.textFields!(1) as UITextField!
}
var createCancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
alert.addTextFieldWithConfigurationHandler { (textField) ->
Void in
textField.placeholder = "Title"
}
alert.addTextFieldWithConfigurationHandler { (textField) ->
Void in
textField.placeholder = "Details"
}
alert.addAction(createItemAction)
alert.addAction(createCancelAction)
self.presentViewController(alert, animated: true, completion: nil)
请尝试在此处使用括号而不是括号:
alert.textFields![0]