【问题标题】:UIAlertView + TextField = Error?UIAlertView + TextField = 错误?
【发布时间】:2015-06-07 12:38:34
【问题描述】:

我正在制作一个应用程序,以便可以将数据保存到表格视图中,并且我想以某种方式使用 UIAlertView 控制器,以便人们可以使用它来创建一个新的单元格(任务),但我知道如何在其中创建一个 textField一个 UIAlertView 但如何使用 textField 数据并在 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 操作中!所以我想要做的主要事情是当用户在文本框中键入并单击退出时,标签将从 textField 更改为该文本,但不使用 viewController!我正在使用 Swift,Xcode 6.3.1

谢谢,

乔治·巴洛

附言很抱歉这个冗长的问题!

【问题讨论】:

    标签: ios swift uialertview


    【解决方案1】:

    在名为ExitUIAlertAction 中(顺便说一句,您应该考虑将名称更改为exitexitAction),您可以通过以下方式访问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")
    }
    

    【讨论】:

      【解决方案2】:

      " .. 您还可以将文本字段添加到警报界面。警报控制器允许您在显示之前提供用于配置文本字段的块。警报控制器维护对每个文本字段的引用,以便您可以访问以后的价值。” 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
      

      *编辑:迅速:

      if let taskTextField = alertController.textFields![0] as! UITextField {
          let taskText = taskTextField.text
          ...
      }
      

      【讨论】:

        【解决方案3】:

        你可以这样做:

        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)
                }
        
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-14
          • 1970-01-01
          • 1970-01-01
          • 2011-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-30
          相关资源
          最近更新 更多