ios - 实现容器 View 的问题

标签 ios objective-c xcode uiviewcontroller uicontainerview

我正在尝试关注 View Controller Programming Guide for iOS在我的应用程序中实现容器 View 。目前我只是想加载一个初始的第一个 View ,但是第一个 Controller 中包含的标签没有显示。最后希望能够通过分段控件来控制在容器中显示哪个view。

如有任何帮助,我们将不胜感激。

我的 Storyboard

Storyboard Screenshot

ViewController.h

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *contentController;

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender;

@end

ViewController.m

#import "ViewController.h"

#import "FirstController.h"
#import "SecondController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    FirstController *firstController = [[FirstController alloc] init];
    [self displayContentController:firstController];
}

- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender
{
}

- (void)displayContentController: (UIViewController *)content
{
    [self addChildViewController:content];

    content.view.frame = [self frameForContentController];

    [self.view addSubview:content.view];

    [content didMoveToParentViewController:self];
}

- (CGRect)frameForContentController
{
    return self.contentController.bounds;
}

@end

最佳答案

如果 FirstController 是 Storyboard 的一部分,那么您必须从 Storyboard 中加载它。

尝试做

FirstController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

关于ios - 实现容器 View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24886770/

相关文章:

swift - dyld : Library not loaded. .. 原因:找不到合适的图像

ios - 无法分配给属性 'observationTime' 是一个只读属性

html - Objective-C 认证

objective-c - 从字符串Objective-C检测类型

objective-c - 如何在同一个委托(delegate)中使用 connectionDidFinishLoading 处理不同的请求?

iphone - 委托(delegate)和通知有什么区别?

ios - 关联类型的依赖注入(inject)导致 Swift 中没有类型名称(下划线)的参数

ios - 如何在自定义 CNContactViewController 中异步更新图像

ios - 转发类作为父类(super class)

ios - 使用 Swift 2.1 在 iOS 中播放两种声音