xcode storyboard容器视图 - 如何访问viewcontroller [英] xcode storyboard Container View - How do I access the viewcontroller
问题描述
我正在尝试使用故事板并使事情正常运行。我已经在我现有的一个视图中添加了一个容器视图。当我尝试在我的视图控制器 .h
文件(ctrl-drag)中添加对此的引用时,我得到一个 IBOutlet UIView * containerView
。如何获取对容器视图的视图控制器的引用?我需要容器视图控制器,所以我可以将它的委托设置为我的视图控制器,这样他们就可以互相交谈。
I'm trying to use storyboard and get things working properly. I've added a a Container View to one of my existing views. When I try to add a reference to this in my view controller .h
file (ctrl-drag), I get a IBOutlet UIView *containerView
. How do I get a reference to the container view's view controller instead? I need the container view controller so I can set it's delegate to my view's controller so they can "talk" to each other.
我的故事板设置为:
在我的.h文件中引用它:
And its referenced in my .h file as:
注意.h中的是UIView,而不是视图的InstallViewController。如何添加对视图控制器的引用?我需要能够设置它的委托。
Notice in the .h that is is a UIView, not my InstallViewController for the view. How do I add a reference to the view controller? I need to be able to set its delegate.
推荐答案
通过指定嵌入segue的标识符,还有另一种解决方案并在方法中检索相应的视图控制器 prepareForSegue:
There is another solution by specifying an identifier for the embed segue(s) and retrieve the corresponding view controllers in method prepareForSegue:
这种方式的优点是你不必依赖由于每个子视图控制器都是通过唯一的segue标识符嵌入的,因此在添加子视图控制器的特定顺序中。
The advantage of this way is that you needn't rely on a specific order in which your child view controllers are added due to the fact that each child view controller is embedded via an unique segue identifier.
更新2013-01-17 - 示例
Update 2013-01-17 - Example
- (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
// -- Master View Controller
if ([segue.identifier isEqualToString:c_SegueIdEmbedMasterVC])
{
self.masterViewController = segue.destinationViewController;
// ...
}
// -- Detail View Controller
else if ([segue.identifier isEqualToString:c_SegueIdEmbedDetailVC])
{
self.detailViewController = segue.destinationViewController;
// ...
}
}
c_SegueIdEmbedMasterVC
& c_SegueIdEmbedDetailVC
是常量,其中包含故事板中定义的segue ID的相应ID。
c_SegueIdEmbedMasterVC
& c_SegueIdEmbedDetailVC
are constants with the corresponding ID of the segue IDs defined in the storyboard.
这篇关于xcode storyboard容器视图 - 如何访问viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!