[英]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
将其添加到
TransactionViewController
的viewDidLoad
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.