繁体   English   中英

从另一个ViewController设置值文本字段

[英]set value textfield from another viewcontroller

我编辑了我的问题,因为set textfield可能不简单,需要引用,所以这是我的代码,但是仍然有问题:

此代码为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)
               }
           }
       }
   }


}

和代码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
        }
    }

}


}

这个问题是:TableViewController中的变量委托似乎总是为零,因此无法设置值

注意:抱歉,我完全编辑了我的问题,因为这么多答案都说不能像这样设置文本字段

您的TransactionViewController所在的上下文尚不完全清楚,但是您正在实例化一个新的ViewController。 如果要引用现有的ViewController,则这里不是您要使用的实例。 如果要创建一个新对象并在didSelectRowAtIndexPath之后显示它,则必须确保TextField在ViewController的init-Method中实例化。 由于您不是从情节提要中实例化它,因此看来您的TransactionViewController是通过编程方式创建的。 可能您仅在viewDidLoad或init()之后的其他内容中设置了TextField。

您正在尝试将text设置为paymentTextField ,但仍未初始化。

为此,您必须在TransactionViewController viewDidLoad方法中将text设置为paymentTextField

didSelectRowAtIndexPath删除transVC.paymentTextField.text = title

将其添加到TransactionViewControllerviewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

   self.paymentTextField.text =  self.payName
}

这不是正确的方法。 您尚未初始化文本字段并尝试设置它的文本。 第一个初始化文本字段:

transVC.paymentTextField = UITextField()

然后尝试做点事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM