ios 将自定义子视图(在 xib 中创建)添加到视图控制器的视图 - 我做错了什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5354653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a custom subview (created in a xib) to a view controller's view - What am I doing wrong
提问by Sefran2
I've created a view in a xib (with an activity indicator, a progress view and a label). Then I've created .h/.m files:
我在 xib 中创建了一个视图(带有活动指示器、进度视图和标签)。然后我创建了 .h/.m 文件:
#import <UIKit/UIKit.h>
@interface MyCustomView : UIView {
IBOutlet UIActivityIndicatorView *actIndicator;
IBOutlet UIProgressView *progressBar;
IBOutlet UILabel *statusMsg;
}
@end
#import "MyCustomView.h"
@implementation MyCustomView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end
In IB, I set the file's owner and view identity to MyCustomView and connect the IBOutlet to the File's owner
在 IB 中,我将文件的所有者和视图标识设置为 MyCustomView 并将 IBOutlet 连接到文件的所有者
In MyViewController.m, I've:
在 MyViewController.m 中,我已经:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame];
[subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
[myTableView addSubview:subView];
[subView release];
}
When I run the app, the view is added, but I can't see the label, the progress bar and the activity indicator.
当我运行应用程序时,视图被添加,但我看不到标签、进度条和活动指示器。
What am I doing wrong?
我究竟做错了什么?
回答by Ned
You need to load it using the -loadNibNamed
method. -initWithNibName
is only for UIViewControllers.
您需要使用该-loadNibNamed
方法加载它。-initWithNibName
仅适用于 UIViewControllers。
Add the following code to your MyCustomView init method:
将以下代码添加到 MyCustomView init 方法中:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];
Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder
to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:
, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).
请记住,如果您从笔尖初始化对象,它会调用- (id)initWithCoder:(NSCoder *)aDecoder
初始化,因此如果您在笔尖中创建 MyCustomView 对象,则必须覆盖它。如果您只是使用initWithFrame:
,那么只需覆盖它并添加上面的代码。此外,在你的笔尖中,确保你有一个顶级 UIView,并将所有其他元素放在其中(确保你的 subviewArray 只有一个条目)。
This will load the views from the nib and add them to the object, and should do the trick.
这将从笔尖加载视图并将它们添加到对象中,并且应该可以解决问题。
回答by hocker
I think you need to use this method:
我认为你需要使用这种方法:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
This is because you need to pass it the .xib filename in the "nibNameOrNil".
这是因为您需要在“nibNameOrNil”中将 .xib 文件名传递给它。
回答by Lorenzo B
As hocker said you have to use that method passing in the XIB name (without the extension).
正如 hocker 所说,您必须使用该方法传入 XIB 名称(不带扩展名)。
Then you have to control this list:
然后你必须控制这个列表:
- Open the .xib file in IB
- Click on File Owner and select the correct class (MyCustomView in your case)
- Hold down control and drag from File Owner to the View (Now the outlet for the view is ok)
- 在 IB 中打开 .xib 文件
- 单击文件所有者并选择正确的类(在您的情况下为 MyCustomView)
- 按住 control 并从 File Owner 拖动到视图(现在视图的出口就可以了)
Hope it works. Cheers.
希望它有效。干杯。