无法从其他 class 设置 UITextfield 的文本

Unable to set the text for UITextfield from other class

我正在尝试使用以下代码为 FirstViewController 中的 SecondViewController 中的 UITextField 设置文本:

// First ViewController
@IBAction func buttonAction(_ sender: Any) {
    let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
    viewController?.sampleTxtField?.text = "world"
    if let navigator = self.navigationController {
        navigator.pushViewController(viewController!, animated: true)
    }
}


// Second ViewController
 @IBOutlet weak var sampleTxtField: UITextField?
override func viewDidLoad() {
    super.viewDidLoad()
    print("the textfield value is,\(String(describing: sampleTxtField?.text))")
}

在我得到的日志中:"the textfield value is,Optional("")"

我想我犯了一些愚蠢的错误。我检查了很多问题,但找不到任何解决方案。

您的代码有误。

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
    viewController?.sampleTxtField?.text = "world"

在上面的语句中,视图未在 UI 中加载,因此 sampleTxtField 始终为 nil,而是在 SecondVC 中创建一个 属性,var sample : String? 并将其分配为多于。然后在SecondVC的viewDidload中,赋值为textField.text = sample

// First ViewController

@IBAction func buttonAction(_ sender: Any) {
    let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
    viewController?.sample = "world"
    if let navigator = self.navigationController {
        navigator.pushViewController(viewController!, animated: true)
    }
}

// 第二个 ViewController

@IBOutlet weak var sampleTxtField: UITextField?
var sample : String?
override func viewDidLoad() {
    super.viewDidLoad()
sampleTxtField.text =  sample
    print("the textfield value is,\(String(describing: sampleTxtField?.text))")
}

在第二个 viewController 中创建一个变量,如下所示:

var sampleText: String!

然后在第二个视图控制器的viewDidLoad()中写下:

sampleTxtField?.text = sampleText

并且在第一个视图控制器中将 viewController?.sampleTxtField?.text = "world" 行替换为:

viewController.sampleText = "world"

这是从另一个 class.

为 textField 设置值的标准方法

当您使用 instantiateViewController(withIdentifier:) 方法从情节提要中实例化 UIViewController 时,它会创建 ViewController 和 returns 的新实例,但 UIView 及其元素在 UIView 加载到内存中之前不会初始化。

ViewDidLoad() gets called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. The view controller’s outlets are guaranteed to have valid values by the time this method is called.

因此您需要在 ViewDidLoad 中配置 UI 元素值,以便为这些元素设置合适的值。

在你的情况下 viewController?.sampleTxtField?.text = "world" 行将无法工作,因为视图尚未加载到内存中。要解决此问题,您需要定义一个 String 变量并在 ViewController 初始化后分配该值。然后在 ViewDidLoad 中,您可以将字符串值分配给 UITextField 文本 属性.

class SecondViewController: UIViewController {

   @IBOutlet weak var sampleTxtField: UITextField!
   var sampleText: String? //it can be var also with default value

   func viewDidLoad() {
     super.viewDidLoad()
     //assign the textField Value here
     if let text = sampleText {
        self.sampleTxtField.text = text 
     }
   }
}

现在在 FirstViewController 中,您可以像

一样分配 sampleText 属性
@IBAction func buttonAction(_ sender: Any) {
    guard let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else {
      fatalError("No ViewController with Identifier "SecondViewController")
    }
    secondViewController.sampleText = "world"
    if let navigator = self.navigationController {
        navigator.pushViewController(secondViewController, animated: true)
    }
}