ios 如何在 Swift 中获取容器内的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29582200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the views inside a container in Swift?
提问by Evan Conrad
I have a Container View that I popped into my storyboard. There's a wonderful little arrow that represents the embed segue to another scene. That scene's top level object is controlled by a custom UIViewController
. I want to call a method that's implemented in my custom class. If I have access to the container, how do I get a reference to what's inside?
我有一个弹出到我的故事板中的容器视图。有一个美妙的小箭头代表嵌入转场到另一个场景。该场景的顶级对象由自定义UIViewController
. 我想调用在我的自定义类中实现的方法。如果我可以访问容器,我如何获得对内部内容的引用?
回答by ABakerSmith
You can use prepareForSegue
, a method in UIViewController
, to gain access to any UIViewController
being segued to from your current view controller, this includes embed
segues.
您可以使用prepareForSegue
中的一个方法UIViewController
来访问UIViewController
当前视图控制器中的任何被embed
隔离的对象,这包括隔离。
From the documentation about prepareForSegue
:
从有关的文档中prepareForSegue
:
The default implementation of this method does nothing. Your view controller overrides this method when it needs to pass relevant data to the new view controller. The segue object describes the transition and includes references to both view controllers involved in the segue.
此方法的默认实现不执行任何操作。当您的视图控制器需要将相关数据传递给新的视图控制器时,它会覆盖此方法。segue 对象描述了转换并包括对 segue 中涉及的两个视图控制器的引用。
In your question you mentioned needing to call a method on your custom view controller. Here's an example of how you could do that:
在您的问题中,您提到需要在自定义视图控制器上调用方法。这是一个如何做到这一点的示例:
1.Give your embed segue a identifier. You can do this in the Interface Builder by selecting your segue, going to the Attributes Editorand looking under Storyboard Embed Segue.
1.给你的 embed segue 一个标识符。您可以在 Interface Builder 中通过选择您的 segue,转到Attributes Editor并查看Storyboard Embed Segue 来执行此操作。
2.Create your classes something like:
2.创建您的类,例如:
A reference is kept to embeddedViewController
so myMethod
can be called later. It's declared to be an implicitly unwrapped optional because it doesn't make sense to give it a non-nil initial value.
保留一个引用,embeddedViewController
以便myMethod
稍后调用。它被声明为一个隐式解包的可选项,因为给它一个非 nil 的初始值是没有意义的。
// This is your custom view controller contained in `MainViewController`.
class CustomViewController: UIViewController {
func myMethod() {}
}
class MainViewController: UIViewController {
private var embeddedViewController: CustomViewController!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CustomViewController,
segue.identifier == "EmbedSegue" {
self.embeddedViewController = vc
}
}
// Now in other methods you can reference `embeddedViewController`.
// For example:
override func viewDidAppear(animated: Bool) {
self.embeddedViewController.myMethod()
}
}
3.Set the classes of your UIViewControllers
in IB using the Identity Inspector. For example:
3.UIViewControllers
使用Identity Inspector设置您在 IB 中的类。例如:
And now everything should work. Hope that helps!
现在一切正常。希望有帮助!
回答by Graham Perks
ABaker's answer gives a great way for the parent to learn about the child. For code in the child to reach the parent, use self.parent
(or in ObjC, parentViewController
).
ABaker 的回答为父母提供了一个了解孩子的好方法。对于要到达父级的子级代码,请使用self.parent
(或在 ObjC 中,parentViewController
)。