set value textfield from another viewcontroller

2.1k views Asked by At

i edited my question , because set textfield maybe can't be simple, need references so this is my code, but still have issue :

this code for TableViewController :

import UIKit

protocol PaymentSelectionDelegate{
func userDidSelectPayment(title: NSString?)
}

class PopPaymentTableViewController: UITableViewController {

    var delegate : PaymentSelectionDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()

    }   

   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath?) {

    self.dismissViewControllerAnimated(true){
        if self.delegate != nil{
            if let ip = indexPath{
                var payItem : PayMethod

                payItem = self.myList[ip.row] as! PayMethod

                var title = payItem.paymentName

               self.delegate.userDidSelectPayment(title)
               }
           }
       }
   }


}

and for code TransactionViewController :

 import UIKit

 class TransactionViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, PaymentSelectionDelegate, UITextFieldDelegate, UIPopoverPresentationControllerDelegate{

 @IBOutlet var paymentTextField: UITextField!

 override func viewDidLoad() {
    super.viewDidLoad()
 }


    func resign(){

        paymentTextField.resignFirstResponder()
   } 

  func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
       if (textField == paymentTextField){
        resign()

        performSegueWithIdentifier("seguePayment", sender: self)
       }
  }

  func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
  }


  func userDidSelectPayment(title: NSString?) {
    paymentTextField.text = title as! String
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "seguePayment"{

        var VC = segue.destinationViewController as! UINavigationController

        var controller  = VC.popoverPresentationController

        if controller != nil{
            controller?.delegate = self

            var paymentVC = PopPaymentTableViewController()
            paymentVC.delegate = self
        }
    }

}


}

this issue is: variable delegate in TableViewController like seems always nil, so cant set value

NB : sorry i edited totally my question, because so many answer say cant be set textfield just like that

3

There are 3 answers

4
Christian On

The context of where your TransactionViewController is, is not completely clear, but you are instantiating a new ViewController. If you want to refer to an existing ViewController, this is not the instance you are using here. If you want to create a new one and show it after your didSelectRowAtIndexPath, you have to make sure, that the TextField is instantiated in the init-Method of your ViewController. As you are not instantiating it from a Storyboard, it seems that your TransactionViewController is created programmatically. Probably you are setting the TextField only in viewDidLoad or something else after the init().

2
iDhaval On

You are trying to set text to paymentTextField which is still no initialized.

For this you have to set text to paymentTextField in viewDidLoad method in TransactionViewController.

remove transVC.paymentTextField.text = title from didSelectRowAtIndexPath

Add it in TransactionViewController's viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

   self.paymentTextField.text =  self.payName
}
2
Nikita Zernov On

It is not correct way to do that. You have not initialised text field and trying to set it's text. First initialise text field:

transVC.paymentTextField = UITextField()

Then try to do something.