Xcode7 UIAlertController 获取用户数据

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

最佳答案

尝试在此处使用方括号而不是圆括号:

    alert.textFields![0]

关于Xcode7 UIAlertController 获取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177790/

相关文章:

ios - 在 Xcode 9 的上下文中,“对象”对于类型查找是不明确的

xcode - NSLog 给我发出不可纠正的警告

xcode - #将函数导入多个源文件

ios - 如何在 UICollectionViewFlowLayout 中检查 uicollectionview 滚动的方向? ( swift )

ios - 触摸 UITextField 之外的任何地方时如何关闭键盘(快速)?

xcode - 断言 UITest 中的按钮上存在图像

SwiftUI = UIViewControllerRepresentable 通过@Binding 更新文本

swift - NSError.setUserInfoValueProvider 无限循环

iphone - 删除最后一个字符 UITextField

iphone - 如果密码正确,如何切换 iPhone View