ios - 如何实例化自定义 iOS UIViewController 并在 Storyboard 下使用它的 NIB?

标签 ios uiviewcontroller storyboard xib nib

StoryBoard setup

我有一个带有 StoryBoard (iOS 5+) 和两个 UIViewController 的项目。

任务是以编程方式实例化第二个 UIViewController (CoordinationController),但我想将它的 XIB 文件用于用户界面设计。

将 XIB 复制到 StoryBoard 的明显解决方法由于未记录的原因不起作用或不可用。 甚至规则是,如果第二个 Storyboard 的 View Controller 没有 View ,它将查找相同名称的 XIB... 不工作。

如何实例化第二个 UIViewController 并使用它的 XIB 文件?

最佳答案

您可以加载 nib 文件并在 initWithCoder: CoordinationController 方法中设置您的 View :

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        UINib *nib = [UINib nibWithNibName:@"CoordinationController" bundle:nil];
        NSArray *nibObjects = [nib instantiateWithOwner:self options:nil];
        self.view = nibObjects.lastObject;
    }
    return self;
}

关于ios - 如何实例化自定义 iOS UIViewController 并在 Storyboard 下使用它的 NIB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15747351/

相关文章:

ios - 更改导航栏标题的自定义嵌入字体的最简单方法是什么

ios - 如何从 pod 下载库文件?

ios - Swift 函数可以只接受一定范围的值吗?

ios - 在 iOS 中使用 Storyboard将图像设置为 TableView 的背景

WPF Storyboard 事件未触发

ios - 如何在 Swift 中将 plist 作为字典获取?

ios - 我需要为新的更大的 iPhone 5 显示器单独的 View Controller 吗?

objective-c - 以编程方式将 UIView 附加到 UIViewController?

ios - 从 xib 文件中的操作按钮推送另一个 View Controller

ios - Storyboard中的 Facebook SSO 实现 - 登录后出现黑屏