将文本从UIAlertController中的textField传递到其他视图

时间:2016-05-08 21:12:02

标签: swift closures uialertcontroller

我有一个带有两个textFields的UIAlertController:

class ModifiedSearchViewController: UISearchController, UISearchBarDelegate {
    var nameOfOwnPlace = ""
    var adressOfOwnPlace = ""

  func insertYourPLace() {

      let alertController = UIAlertController(title: "Insert New Place", message: "", preferredStyle: UIAlertControllerStyle.Alert)
      let saveAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {
        alert -> Void in

          let nameTextField = alertController.textFields![0] as UITextField
          let adressTextField = alertController.textFields![1] as UITextField
          self.nameOfOwnPlace = nameTextField.text!
          self.adressOfOwnPlace = adressTextField.text!
          print(self.nameOfOwnPlace)

      })

      let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {
        (action : UIAlertAction!) -> Void in

      })

      alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
          textField.placeholder = "Enter Place's Name..."
      }
      alertController.addTextFieldWithConfigurationHandler { (textField : UITextField!) -> Void in
          textField.placeholder = "Enter Adress's Name..."
      }

      alertController.addAction(saveAction)
      alertController.addAction(cancelAction)
      self.presentViewController(alertController, animated: true, completion: nil)

  }
...
}

我有一个使用我的ModifiedSearchViewController类的其他视图 但我没有成功将我的字符串(nameOfOwnPlace,adressOfOwnPlace)发送到另一个视图

 searchController = ModifiedSearchViewController(searchResultsController: resultsViewController)
print(searchController.adressOfOwnPlace)  // nil

感谢您的帮助! :)

EDIT ::

var resultsViewController = GMSAutocompleteResultsViewController()
var searchController = ModifiedSearchViewController()

func setSearchViewController() {
            resultsViewController.delegate = self
            searchController = ModifiedSearchViewController(searchResultsController: resultsViewController)
            searchController.searchResultsUpdater = resultsViewController

            print(searchController.adressOfOwnPlace)  // nil
}

0 个答案:

没有答案