ios - Swift:如何向 UIAlertController 中的 UITextView 添加名称?

标签 ios xcode swift swift2 xcode7

所以我在 UIAlertController 中有一个 UITextView ,稍后在代码中我编写了以下内容:

        for(var i: Int = 0; i < self.codesdemo.count; i++) {
        if let code = self.textField.text {
            if code == codesdemo[i] {
                // This should present view controller if codes is real/worked
                let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
                let viewController = storyboard.instantiateViewControllerWithIdentifier("Demo") as UIViewController
                self.presentViewController(viewController, animated: true, completion: nil)
            }
        }
    }
    }))}

基本上是说,如果有人在 textView 中写入 [i],它将转到另一个 View Controller 。 这是我相关的完整代码:

@IBAction func EnterCode(sender: AnyObject) {

    let alert = UIAlertController(title: "Enter Code", message: "Enter a code to access your chatroom", preferredStyle: .Alert)
    alert.addTextFieldWithConfigurationHandler { (textField:UITextField) -> Void in
        textField.placeholder = "Enter Code"
    }

    alert.addAction(UIAlertAction(title: "Enter", style: .Default, handler: { (action:UIAlertAction) -> Void in
        let textField = alert.textFields!.first!






    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)


    //  Loops through the codes array
    for(var i: Int = 0; i < self.codesdemo.count; i++) {
        if let code = self.textField.text {
            if code == codesdemo[i] {
                // This should present view controller if codes is real/worked
                let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
                let viewController = storyboard.instantiateViewControllerWithIdentifier("Demo") as UIViewController
                self.presentViewController(viewController, animated: true, completion: nil)
            }
        }
    }
    }))}

在本节中: if let code = self.textField.text { 我收到一条错误消息:值类型 Homeviewcontroller (即 View Controller )没有 textField 的成员。 我不明白,因为我我想,当我在 UIAlertController 中创建 textField 时,它被称为 textField 我想知道是否需要做一些事情来重命名它,以便我能够修复此错误。有人可以帮忙吗?

最佳答案

您正在尝试在操作处理程序 block 中呈现 UIAlertController ..这就是您的警报 View 不会出现在 View 中的原因: 我编辑了按钮操作方法的一些代码行..这是代码

@IBAction func EnterCode(sender: AnyObject) {

        let alert = UIAlertController(title: "Enter Code", message: "Enter a code to access your chatroom", preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler{ (textField:UITextField!) -> Void in
            textField.placeholder = "Enter Code"
        } 

        alert.addAction(UIAlertAction(title: "Enter", style: .Default, handler: { (action:UIAlertAction!) -> Void in
            let textField: UITextField = alert.textFields!.first! as UITextField

            alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

            for(var i: Int = 0; i < self.codesdemo.count; i++) {
                if let code: String = textField.text {
                    if code == codesdemo[i] {
                        // This should present view controller if codes is real/worked
                        //other code here 
                    }
                }
            }

        }))
        self.presentViewController(alert, animated: true, completion: nil)

    }

关于ios - Swift:如何向 UIAlertController 中的 UITextView 添加名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36316928/

相关文章:

iphone - 静态 NSMutableDictionary

ios - UIImageView 不动

ios - 如何快速更改搜索栏的高度

ios - 将输入数字存储在文本框中的变量中

ios - React Native 推送通知显示在 XCode 控制台中,但不显示在设备上

ios - 如何使用 Alamofire 制定一个良好的模式来控制多个请求依赖关系?比如request2需要request1的响应

ios - 自定义 UICollectionViewLayout 标题和 Storyboard

iphone - 如何在 ContainerView 内部的 ViewController 之间切换?

ios - Storyboard中 View Controller 之间的共享实例

ios - 添加对象 : to NSMutableArray and sending to TableView