Swift: 如何为 UIAlertController 中的 UITextView 添加名称?
Swift: How do I add a name to the UITextView in a UIAlertController?
所以我在 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],它会转到另一个 viewcontroller。
这是我的完整代码:
@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
(即 viewcontroller)没有 textField
的成员。 我不明白,因为我想当我在 UIAlertController
中创建文本字段时它被称为 textField
我想知道是否需要做些什么来重命名它以便我能够修复此错误。有人可以帮忙吗?
您正在尝试在操作处理程序块中显示 UIAlertController。这就是您的 alertview 不会出现在视图中的原因:
我已经编辑了按钮操作方法的一些代码行..这里是代码
@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)
}
所以我在 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],它会转到另一个 viewcontroller。 这是我的完整代码:
@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
(即 viewcontroller)没有 textField
的成员。 我不明白,因为我想当我在 UIAlertController
中创建文本字段时它被称为 textField
我想知道是否需要做些什么来重命名它以便我能够修复此错误。有人可以帮忙吗?
您正在尝试在操作处理程序块中显示 UIAlertController。这就是您的 alertview 不会出现在视图中的原因: 我已经编辑了按钮操作方法的一些代码行..这里是代码
@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)
}