简体   繁体   English

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

[英]set value textfield from another viewcontroller

i edited my question , because set textfield maybe can't be simple, need references so this is my code, but still have issue : 我编辑了我的问题,因为set textfield可能不简单,需要引用,所以这是我的代码,但是仍然有问题:

this code for TableViewController : 此代码为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 : 和代码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 这个问题是:TableViewController中的变量委托似乎总是为零,因此无法设置值

NB : sorry i edited totally my question, because so many answer say cant be set textfield just like that 注意:抱歉,我完全编辑了我的问题,因为这么多答案都说不能像这样设置文本字段

The context of where your TransactionViewController is, is not completely clear, but you are instantiating a new ViewController. 您的TransactionViewController所在的上下文尚不完全清楚,但是您正在实例化一个新的ViewController。 If you want to refer to an existing ViewController, this is not the instance you are using here. 如果要引用现有的ViewController,则这里不是您要使用的实例。 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. 如果要创建一个新对象并在didSelectRowAtIndexPath之后显示它,则必须确保TextField在ViewController的init-Method中实例化。 As you are not instantiating it from a Storyboard, it seems that your TransactionViewController is created programmatically. 由于您不是从情节提要中实例化它,因此看来您的TransactionViewController是通过编程方式创建的。 Probably you are setting the TextField only in viewDidLoad or something else after the init(). 可能您仅在viewDidLoad或init()之后的其他内容中设置了TextField。

You are trying to set text to paymentTextField which is still no initialized. 您正在尝试将text设置为paymentTextField ,但仍未初始化。

For this you have to set text to paymentTextField in viewDidLoad method in TransactionViewController . 为此,您必须在TransactionViewController viewDidLoad方法中将text设置为paymentTextField

remove transVC.paymentTextField.text = title from didSelectRowAtIndexPath didSelectRowAtIndexPath删除transVC.paymentTextField.text = title

Add it in TransactionViewController 's viewDidLoad 将其添加到TransactionViewControllerviewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

   self.paymentTextField.text =  self.payName
}

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. 然后尝试做点事情。

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

相关问题 从另一个viewController访问文本字段文本 - access textfield text from another viewController Xcode-从不同viewController中的文本字段检索值 - Xcode - Retrieving value from textfield in different viewController 从UrlScheme在ViewController上设置值 - Set value on ViewController from UrlScheme 在Swift中从ViewController将TextField委托设置为另一个类不起作用 - Setting TextField Delegate to another Class from ViewController in Swift is not working 将带有textField的图像传递到另一个viewController - pass image with textField to another viewController 为什么不能通过使用另一个viewController设置viewController的变量值? - Why can't set a variable value of a viewController by using another viewController? TextField 文本需要从另一个 ViewController 的 TextField 输入镜像文本 - TextField text needs to mirror text from another ViewController’s TextField input 从另一个Storyboard设置ViewController的属性 - Set Property of ViewController from Another Storyboard 在UIWebView上的viewcontroller中从另一个viewcontroller隐藏值 - Hide value inside viewcontroller on UIWebView from another viewcontroller 从一个ViewController传递一个整数值到另一个ViewController并在UITextField中显示它 - Pass an integer value from one ViewController to another ViewController and display it in UITextField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM