How to load a UIView using a nib file created with Interface Builder(如何使用使用 Interface Builder 创建的 nib 文件加载 UIView)
问题描述
我正在尝试做一些复杂的事情,但应该是可能的.所以这里对你们所有的专家来说都是一个挑战(这个论坛有很多人:)).
I'm trying to do something a bit elaborate, but something that should be possible. So here is a challenge for all you experts out there (this forum is a pack of a lot of you guys :) ).
我正在创建一个问卷组件",我想将它加载到 NavigationContoller
(我的 QuestionManagerViewController
)上.组件"是一个空"的UIViewController
,可以根据需要回答的问题加载不同的视图.
I'm creating a Questionnaire "component", which I want to load on a NavigationContoller
(my QuestionManagerViewController
). The "component" is an "empty" UIViewController
, which can load different views depending on the question that needs to be answered.
我的做法是:
- 将 Question1View 对象创建为
UIView
子类,定义一些IBOutlets
. - 创建(使用 Interface Builder)
Question1View.xib
(这就是我的问题可能在哪里).我将UIViewController
和UIView
都设置为 Question1View 类. - 我将 outlet 与视图的组件链接(使用 IB).
我将
QuestionManagerViewController
的initWithNib
覆盖为如下所示:
- Create Question1View object as a
UIView
subclass, defining someIBOutlets
. - Create (using Interface Builder) the
Question1View.xib
(HERE IS WHERE MY PROBLEM PROBABLY IS). I set both theUIViewController
and theUIView
to be of class Question1View. - I link the outlets with the view's component (using IB).
I override the
initWithNib
of myQuestionManagerViewController
to look like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:@"Question1View" bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
当我运行代码时,我收到了这个错误:
When I run the code, I'm getting this error:
2009-05-14 15:05:37.152 iMobiDines[17148:20b] *** 由于未捕获的异常NSInternalInconsistencyException
"而终止应用程序,原因:-[UIViewController _loadViewFromNibNamed:bundle:]
加载了Question1View" nib,但未设置视图出口.'
2009-05-14 15:05:37.152 iMobiDines[17148:20b] *** Terminating app due to uncaught exception '
NSInternalInconsistencyException
', reason: '-[UIViewController _loadViewFromNibNamed:bundle:]
loaded the "Question1View" nib but the view outlet was not set.'
我确信有一种方法可以使用 nib 文件加载视图,而无需创建 viewController 类.
I'm sure there is a way to load the view using the nib file, without needing to create a viewController class.
推荐答案
还有一种更简单的方法可以访问视图,而不是将 nib 作为数组处理.
There is also an easier way to access the view instead of dealing with the nib as an array.
1) 创建一个自定义 View 子类,其中包含您以后想要访问的任何插座.--我的视图
1) Create a custom View subclass with any outlets that you want to have access to later. --MyView
2) 在要加载和处理 nib 的 UIViewController 中,创建一个 IBOutlet 属性来保存加载的 nib 的视图,例如
2) in the UIViewController that you want to load and handle the nib, create an IBOutlet property that will hold the loaded nib's view, for instance
在 MyViewController(一个 UIViewController 子类)中
in MyViewController (a UIViewController subclass)
@property (nonatomic, retain) IBOutlet UIView *myViewFromNib;
(不要忘记合成它并在你的 .m 文件中发布)
(dont forget to synthesize it and release it in your .m file)
3) 在 IB 中打开您的 nib(我们称之为myViewNib.xib"),将文件的所有者设置为 MyViewController
3) open your nib (we'll call it 'myViewNib.xib') in IB, set you file's Owner to MyViewController
4) 现在将文件的 Owner outlet myViewFromNib 连接到 nib 中的主视图.
4) now connect your file's Owner outlet myViewFromNib to the main view in the nib.
5) 现在在 MyViewController 中,编写以下行:
5) Now in MyViewController, write the following line:
[[NSBundle mainBundle] loadNibNamed:@"myViewNib" owner:self options:nil];
现在,只要您这样做,调用您的属性self.myViewFromNib"即可让您从您的 nib 访问视图!
Now as soon as you do that, calling your property "self.myViewFromNib" will give you access to the view from your nib!
这篇关于如何使用使用 Interface Builder 创建的 nib 文件加载 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用使用 Interface Builder 创建的 nib 文件加载 UIView
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01