gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:33:43 29 4
gpt4 key购买 nike

所以我在 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com