仅将数据传递给UINavigation上的静态故事板嵌入式tableview控制器

问题描述 投票:0回答:1

因此,我所有的控制器都是以编程方式完成的,以避免出现串扰和此类复杂的情况。

我有一个从网络下载数据的viewcontroller(称为ProfileViewController)。

因此,我在ProfileViewController中有一个方法实例化带有静态tableview的单个情节提要文件,其中包含具有文本字段的单元格。这是方法:

ProfileViewController:

func userSelectedUpdateProfile() {

   // Obtain reference to the only storyboard file named EditProfileSB
   let storyboard = UIStoryboard(name: "EditProfileSB", bundle: nil)

   // Since the Tableview is embedded in a navigation controller (with ID set to "navigationID")
   if let parentNavigationController = storyboard.instantiateViewController(withIdentifier: "navigationID") as? UINavigationController {

        // Now find the embedded TableViewController and access it's properties to pass to.
        if let childEditController = parentNavigationController.topViewController as? EditProfileTableViewController {

            // ! Error here ! Found nil when doing this.
            childEditController.nameTextfield.text = "Passed this to static cell"

        }
       present(parentNavigationController, animated: true, completion: nil)
    }
}

因此,代码本身对于我在这里想要实现的目的不言自明。 TableView嵌入在导航中(在情节提要中使用“编辑器>嵌入”完成),因此在第二个嵌套的if let语句中,我现在正在检查以查找该Edit控制器并访问其属性(nameTextfield)。

[当我尝试访问nameTextField.text属性时发生崩溃。此文本字段是使用情节提要设置的。这是EditProfileTableViewController:

class EditProfileTableViewController: UITableViewController {

  @IBOutlet weak var nameTextfield: UITextField!

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  // Other methods ...
}

这里是情节提要板流布局。enter image description here

我在这里想念什么吗?我在方法childEditController.nameTextfield.text = "Passed this to static cell"上的userSelectedUpdateProfile()上经常崩溃。

ios swift storyboard segue uistoryboardsegue
1个回答
0
投票

如果您的View Controller仍然不调用viewDidLoad()。您的文本字段未创建。

@IBOutlet weak var nameTextfield: UITextField!

您可以看到它的属性很弱。尝试创建一个值并将文本传递给该值。然后,在viewDidLoad()中,可以将值设置为textField

© www.soinside.com 2019 - 2024. All rights reserved.