2

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

fendy
  • 199
  • 2
  • 3
  • 11

3 Answers3

0

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().

Christian
  • 4,596
  • 1
  • 26
  • 33
  • paymentTextField its already declare at TransactionViewController. this flow maybe tell like this : in TransactionViewController have textfield which if you click at the textfield it will show popover tableViewController, and if you selected cell, value in cell its inject to textfield – fendy Jun 18 '15 at 07:34
  • than you should give your tableViewController a reference to your TransactionViewController and use this reference instead of creating a new instance of the TransactionViewController. – Christian Jun 18 '15 at 07:38
  • maybe reference what you mean its protocol ? i already edit , take a look . thx – fendy Jun 18 '15 at 08:50
  • nearly. In `prepareForSegue` you are setting the delegate for a new instance of your `PopPaymentTableViewController`. If this controller is defined in your storyboard as rootViewController of the `NavigationController` of the seague, you can get the instance by controller.viewControllers[0] – Christian Jun 18 '15 at 09:28
0

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
}
iDhaval
  • 3,175
  • 1
  • 11
  • 21
  • yeah, but this scene little bit different , in TransactionViewController have textfield which if you click at the textfield it will show popover tableViewController, and if you selected cell, value in cell its inject to textfield, so i can't used in ViewDidLoad maybe – fendy Jun 18 '15 at 07:31
  • @fendy this will not make any difference. – iDhaval Jun 18 '15 at 07:38
0

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.

Nikita Zernov
  • 5,465
  • 6
  • 39
  • 70