我想为 iOS5 开发并有一个 Storyboard... 我刚刚在 Storyboard上使用 UIViewController 创建了 UIView。 我添加了许多其他 UIButtons 和标签,并创建了 VC socket 。
我想在单个父 View 上将此 View 与它的 View Controller 一起使用 3 次。 这怎么可能?我不想复制“很多其他 UIButtons 和标签”...
也许我应该在单独的 XIB 中从 Storyboard 中创建这个 View ?我将如何在 Storyboard中使用 XIB?
更新:
谢谢你,Juzzz - 您的解决方案完美无缺:
最佳答案
您必须创建 2 个自定义 View Controller (一个用于 Storyboard中的每个 View 。 示例:
@interface GraphCollectionViewController : UIViewController
@interface GraphViewController : UIViewController
要连接它们,您可以使用一种名为:UIViewController containment 的东西
为您的 GraphCollectionViewController 创建 3 个用于 UIView 的 socket 。然后创建 GraphViewController 的 3 个属性(或一个数组,你想要什么)并在加载的 View 中初始化它们。
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) IBOutlet UIView *middleView;
@property (strong, nonatomic) IBOutlet UIView *bottomView;
@property (strong, nonatomic) GraphViewController *topGraphViewController;
@property (strong, nonatomic) GraphViewController *middleGraphViewController;
@property (strong, nonatomic) GraphViewController *bottomGraphViewController;
...
//top graph
self.topGraphViewController = [storyboard instantiateViewControllerWithIdentifier:@"GraphViewController"]; //init with view from storyboard
self.topGraphViewController.view.frame = self.topView.bounds; //set frame the same
[self.view addSubview:self.topGraphViewController.view];
[self addChildViewController:self.topGraphViewController];
[self.topGraphViewController didMoveToParentViewController:self];
//middle graph
self.middleGraphViewController = [storyboard instantiateViewControllerWithIdentifier:@"GraphViewController"]; //init with view from storyboard
self.middleGraphViewController.view.frame = self.middleView.bounds; //set frame the same
[self.view addSubview:self.middleGraphViewController.view];
[self addChildViewController:self.middleGraphViewController];
[self.middleGraphViewController didMoveToParentViewController:self];
//bottom graph
self.bottomGraphViewController = [storyboard instantiateViewControllerWithIdentifier:@"GraphViewController"]; //init with view from storyboard
self.bottomGraphViewController.view.frame = self.bottomView.bounds; //set frame the same
[self.view addSubview:self.bottomGraphViewController.view];
[self addChildViewController:self.bottomGraphViewController];
[self.bottomGraphViewController didMoveToParentViewController:self];
我想你明白了。更多了解this example .
关于ios - 如何使用 iOS5 的 viewcontroller 创建通用 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10148639/