Xcode7 UIAlertController获取用户数据

时间:2015-10-16 18:59:16

标签: xcode swift uitextfield xcode7 uialertcontroller

我需要一些帮助来从条形按钮提取自定义警报。目标是弹出警报,要求用户输入标题和说明。警报将有两个文本字段,一个用于标题,一个用于详细信息,带有“创建”和“取消”按钮。

代码如下,但我在Xcode 7.0.1中遇到两个相同的错误:

无法调用非函数类型'[UITextField]'

的值

这些错误发生在以下几行:

     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)

1 个答案:

答案 0 :(得分:0)

尝试使用括号代替括号:

    alert.textFields![0]