UIAlertView + TextField = 错误?
UIAlertView + TextField = Error?
我正在制作一个应用程序,以便可以将数据保存到 Table 视图中,我想以某种方式使用 UIAlertView 控制器,以便人们可以使用它来创建新的 Cell(任务),但我知道如何在 UIAlertView 中制作文本字段,但如何使用文本字段数据并在 UIAlertView 操作按钮中使用它!
@IBAction func addNewTask() {
var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("User Exited The App!")
exit(2)
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
// Here you can configure the text field (eg: make it secure, add a placeholder, etc)
}
// Add the actions
alertController.addAction(Exit)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
这是代码,但我如何使用 alertController.addTextFieldWithConfigureationHandler
中的数据并将其放入 Exit
操作中!所以我想让它做的主要事情是,当用户在文本框中键入内容并单击“退出”时,标签将更改为文本字段中的该文本,但不使用 viewController!我正在使用 Swift、Xcode 6.3.1
谢谢,
乔治·巴洛
p.s。抱歉这个冗长的问题!
在名为 Exit
的 UIAlertAction
中(顺便说一句,您应该考虑将名称更改为 exit
或 exitAction
之类的名称),您可以访问 UITextField
及其内容如下:
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
let loginTextField = alertController.textFields![0] as! UITextField
let inputText = loginTextField.text
println("The input text is: \(inputText)")
NSLog("OK Pressed")
}
" . ..您还可以将文本字段添加到警报界面。警报控制器允许您提供一个块,用于在显示之前配置文本字段。警报控制器维护对每个文本字段的引用,以便您以后可以访问它的值。” https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController
UIAlertViewController 上有一个您可以访问的文本字段 属性。
alertController.textFields
文本字段添加到此数组的顺序与您将它们添加到警报控制器的顺序相同。
在您完成退出操作的内部:
UITextField *taskTextField = alertController.textFields.firstObject
*编辑:在swift中:
if let taskTextField = alertController.textFields![0] as! UITextField {
let taskText = taskTextField.text
...
}
你可以这样做:
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
var mytextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
addNewTask()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addNewTask() {
var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
self.myLabel.text = self.mytextField.text
}
var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("User Exited The App!")
exit(2)
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
// Here you can configure the text field (eg: make it secure, add a placeholder, etc)
self.mytextField = textField
}
// Add the actions
alertController.addAction(Exit)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
}
我正在制作一个应用程序,以便可以将数据保存到 Table 视图中,我想以某种方式使用 UIAlertView 控制器,以便人们可以使用它来创建新的 Cell(任务),但我知道如何在 UIAlertView 中制作文本字段,但如何使用文本字段数据并在 UIAlertView 操作按钮中使用它!
@IBAction func addNewTask() {
var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("User Exited The App!")
exit(2)
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
// Here you can configure the text field (eg: make it secure, add a placeholder, etc)
}
// Add the actions
alertController.addAction(Exit)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
这是代码,但我如何使用 alertController.addTextFieldWithConfigureationHandler
中的数据并将其放入 Exit
操作中!所以我想让它做的主要事情是,当用户在文本框中键入内容并单击“退出”时,标签将更改为文本字段中的该文本,但不使用 viewController!我正在使用 Swift、Xcode 6.3.1
谢谢,
乔治·巴洛
p.s。抱歉这个冗长的问题!
在名为 Exit
的 UIAlertAction
中(顺便说一句,您应该考虑将名称更改为 exit
或 exitAction
之类的名称),您可以访问 UITextField
及其内容如下:
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
let loginTextField = alertController.textFields![0] as! UITextField
let inputText = loginTextField.text
println("The input text is: \(inputText)")
NSLog("OK Pressed")
}
" . ..您还可以将文本字段添加到警报界面。警报控制器允许您提供一个块,用于在显示之前配置文本字段。警报控制器维护对每个文本字段的引用,以便您以后可以访问它的值。” https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/index.html#//apple_ref/occ/cl/UIAlertController
UIAlertViewController 上有一个您可以访问的文本字段 属性。
alertController.textFields
文本字段添加到此数组的顺序与您将它们添加到警报控制器的顺序相同。
在您完成退出操作的内部:
UITextField *taskTextField = alertController.textFields.firstObject
*编辑:在swift中:
if let taskTextField = alertController.textFields![0] as! UITextField {
let taskText = taskTextField.text
...
}
你可以这样做:
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
var mytextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
addNewTask()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addNewTask() {
var alertController = UIAlertController(title: "Add A New Task", message: "Add The Correct Information To Add A New Task!", preferredStyle: .Alert)
// Create the actions
var Exit = UIAlertAction(title: "Add Task", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
self.myLabel.text = self.mytextField.text
}
var cancelAction = UIAlertAction(title: "Exit App", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("User Exited The App!")
exit(2)
}
alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
// Here you can configure the text field (eg: make it secure, add a placeholder, etc)
self.mytextField = textField
}
// Add the actions
alertController.addAction(Exit)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
}
}