【问题标题】:Is there any way to get the textfield information of a container?有没有办法获取容器的文本字段信息?
【发布时间】:2016-04-16 00:57:50
【问题描述】:

拥有两个viewcontroller是第二个容器。 当试图获取容器中的文本字段时,我得到的返回是 nil。

有没有办法获取容器的textfield信息?

print

【问题讨论】:

    标签: swift swift2 containers


    【解决方案1】:

    当将一个视图控制器放入故事板中的容器时,会创建一个嵌入转场。您的包含视图控制器可以通过prepareForSegue() 访问包含的视图控制器,如下所示:

    var weak containedTextField: UITextField!
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "<whatever-ID-you-set-on-your-segue>" {
            if let containedViewController = segue.destinationViewController as? <your-embedded-view-controller's-type> {
                containedTextField = containedViewController.textField
            }
        }
    }
    

    然后,您的包含视图控制器可以根据需要使用 textField 实例。

    【讨论】:

    • 我在下面的代码中使用了这种方法,但它不起作用。 func override prepareForSegue (follows: UIStoryboardSegue, sender: AnyObject) { if segue.identifier == "segueID" { if let containsViewController = segue.destinationViewController as? firstViewController { containsViewController.varfirstview = TextField.text } } }
    • segue.destinationViewController as? firstViewController 甚至不应该编译。
    • 我不明白。我处理了这个例子,你经历了所有的回报都是零的方式。即使尝试在自己的视图中获取文本,回报也是零。
    【解决方案2】:

    我找到的解决方案是以编程方式将自定义视图(容器)替换为字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2022-01-06
      • 1970-01-01
      • 2021-03-09
      相关资源
      最近更新 更多