我在试着拿出UIAlertController分和UITextView分.当我添加该行时:

    //Add text field
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
    }        

我得到了Runtime个错误:

致命错误:展开可选值时意外发现空

    let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert)

    //cancel button
    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
        //cancel code
    }
    alertController.addAction(cancelAction)

    //Create an optional action
    let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in
        let text = (alertController.textFields?.first as! UITextField).text
        println("You entered \(text)")
    }
    alertController.addAction(nextAction)

    //Add text field
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        textField.textColor = UIColor.greenColor()
    }
    //Present the AlertController
    presentViewController(alertController, animated: true, completion: nil)

这是通过IBAction在my ViewController中呈现的.

我已经从here下载了代码,它工作得很好.我将该方法复制并粘贴到我的代码中,但它崩溃了.self在最后一条线路上的存在没有任何影响.

推荐答案

SWIFT 5.1

alert.addTextField { (textField) in
    textField.placeholder = "Enter First Name"
}

使用此代码,我正在我的应用程序中成功运行此代码.

@IBAction func addButtonClicked(sender : AnyObject){
    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter Second Name"
    }
    let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { alert -> Void in
        let firstTextField = alertController.textFields![0] as UITextField
        let secondTextField = alertController.textFields![1] as UITextField
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
        (action : UIAlertAction!) -> Void in })
    alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter First Name"
    }
    
    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)
    
    self.presentViewController(alertController, animated: true, completion: nil)
}

编辑:Swift 3.0版

@IBAction func addButtonClicked(_ sender: UIButton){
    let alertController = UIAlertController(title: "Add New Name", message: "", preferredStyle: .alert)
    alertController.addTextField { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter Second Name"
    }
    let saveAction = UIAlertAction(title: "Save", style: .default, handler: { alert -> Void in
        let firstTextField = alertController.textFields![0] as UITextField
        let secondTextField = alertController.textFields![1] as UITextField
        print("firstName \(firstTextField.text), secondName \(secondTextField.text)")
    })
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { (action : UIAlertAction!) -> Void in })
    alertController.addTextField { (textField : UITextField!) -> Void in
        textField.placeholder = "Enter First Name"
    }

    alertController.addAction(saveAction)
    alertController.addAction(cancelAction)
    
    self.present(alertController, animated: true, completion: nil)
}

Ios相关问答推荐

构建iOS项目失败.我们运行了";xcodeBuild";命令,但它退出并返回错误代码65-expo reaction ative

Xcode15.3上的生成错误:调用的对象类型';facebook::flipper::SocketCertificateProvider';不是函数或函数指针

SWIFT根据地球坐标修改 node 旋转

如何在wiftUI中管理导航栈?

Swift locationManager.requestWhenInUseAuthorization() 不提示用户

Swift:判断 Foundation (NS)Errors 与自定义协议的一致性

RealityKit – ARView 光线投射返回 0 个结果

如何在 SwiftUI 中创建两个大小相同的正方形占据整个屏幕宽度?

@Environment 的存在会导致列表在滚动时不断重建其内容

将 SwiftUI 与 @AppStorage 和动态函数调用一起使用

UIButton在iOS7中没有显示突出显示

UITableView 页脚,停止浮动内容

iOS swift从另一个数组中删除一个数组的元素

iOS 10 错误:UICollectionView 接收到具有不存在索引路径的单元格的布局属性

interfaceOrientation在 iOS 8 中已弃用,如何更改此方法 Objective C

如何隐藏uitabbar控制器

iOS Swift - 获取当前本地时间和日期时间戳

'无效更新:第 0 节中的无效行数

Objective-C 将 NSDate 设置为当前 UTC

在 SKScene 中设置按钮