ios - 如何使用 iOS5 的 viewcontroller 创建通用 View ?

标签 ios ios5 uiview uiviewcontroller storyboard

我想为 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/

相关文章:

ios - 水平视差滚动 UIScrollView iOS

xcode - 当附件为UITableViewCellAccessoryCheckmark时如何在UITableViewCell中居中放置文本

iOS - 在堆栈 View 中以编程方式添加垂直线

ios - Storyboard和程序化 View

ios - 如何在 iOS 7 上模仿键盘动画将 "Done"按钮添加到数字键盘?

ios - 在 Swift 中打印有关解码失败的 DecodingError 详细信息

ios - 我需要在 objective-c 中创建一个 3D 旋转框

ios - Xcode 8 beta 4 中的 shouldAutorotate() 函数

objective-c - 将图像从iOS上传到Grails

ios - 如何从一个手势识别器转到另一个手势识别器