xcode iOS - 将自定义 UIView 放在视图控制器中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14911899/
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
iOS - put custom UIView in a View Controller
提问by chenxee
My iOS app has a root view controller, and I want to add a few custom UIView
to it in runtime. I call these custom views widgets because they are small and basically pretty same except size, label text etc.
我的 iOS 应用程序有一个根视图控制器,我想UIView
在运行时向它添加一些自定义。我称这些自定义视图小部件是因为它们很小并且除了大小、标签文本等之外基本相同。
I have created .h and .m for my widgets, and .xib file. In my root view controller viewDidLoad
I do this:
我已经为我的小部件创建了 .h 和 .m 以及 .xib 文件。在我的根视图控制器中,viewDidLoad
我这样做:
TestMyView *cell = [[[NSBundle mainBundle] loadNibNamed:@"TestMyView" owner:self options:nil] objectAtIndex:0];
[self.view addSubview: cell];
It works fine. However, I still can't figure out:
它工作正常。但是,我仍然无法弄清楚:
- why there's exception
"[<TestViewController 0x7517210> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mylabel"
if I addUILabel
mylabel as IBOutlet to my custom UIView class .h file. (I drag drop UILabel in IB then control-drag to .h) I need to change label text when view is created. And I dont quite understand why it complains TestViewController while I didn't add IBOutlet to it, but my custom view .h class? - how do I change label text in right way then. I dont want to create a View controller for my widgets because I think it wouldn't be necessary. I need to create like 5-6 widgets so I need to create 5-6 view controllers for them?
- it seems initWithCoder doesnt get called when I create the widget? I saw apple doc library says if load view from nib file this method will get called.
"[<TestViewController 0x7517210> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mylabel"
如果我将UILabel
mylabel 作为 IBOutlet添加到我的自定义 UIView 类 .h 文件中,为什么会出现异常。(我在 IB 中拖放 UILabel 然后控制拖动到 .h)我需要在创建视图时更改标签文本。而且我不太明白为什么它会抱怨 TestViewController 而我没有向它添加 IBOutlet,但是我的自定义视图 .h 类?- 那么如何以正确的方式更改标签文本。我不想为我的小部件创建视图控制器,因为我认为没有必要。我需要创建 5-6 个小部件,所以我需要为它们创建 5-6 个视图控制器?
- 当我创建小部件时,似乎没有调用 initWithCoder?我看到苹果文档库说如果从 nib 文件加载视图,这个方法将被调用。
If you think I suck at explain this, here's my project code: https://dl.dropbox.com/u/43017476/custom.tar.gz
如果你认为我解释这个很烂,这是我的项目代码:https: //dl.dropbox.com/u/43017476/custom.tar.gz
回答by Dave
You're close. A few notes.
你很接近。一些笔记。
1st - when you load the view from the nip, the "owner" is the class which has the properties (IBOutlets) references by the nib. That will probably be an instance of TestMyView. Usually, create the custom view and let it load it's own nib (as illustrated below).
第一个 - 当您从 nip 加载视图时,“所有者”是具有 nib 引用的属性 (IBOutlets) 的类。那可能是 TestMyView 的一个实例。通常,创建自定义视图并让它加载自己的笔尖(如下图所示)。
2nd - loadNibNamed returns an NSARRAY, not a UIVIEW; so you'll want to pull your view out of the array.
第二 - loadNibNamed 返回 NSARRAY,而不是 UIVIEW;所以你会想把你的视图从数组中拉出来。
Here's an example of creating your custom view (widget):
这是创建自定义视图(小部件)的示例:
CGRect frame = CGRectMake(nextLeft, nextTop, tileWidth, tileHeight);
TestMyView *widget = [[TestMyView alloc] initWithFrame:frame andFoo:foo];
[widget setWidgetTappedBlock:^{
// handle tap for this widget
}];
[self.widgetContainerView addSubview:widget];
This is part of your custom views implemenatation:
这是您的自定义视图实现的一部分:
@implementation TestMyView
- (id)initWithFrame:(CGRect)frame andFoo:(Foo *)foo
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
NSArray *nibObjects=[[NSBundle mainBundle] loadNibNamed:@"TestMyView" owner:self options:nil];
[self addSubview:[nibObjects objectAtIndex:0]];
// More initialization code
}
return self;
}
@end
And the interface:
和界面:
@interface ProductTileView : UIView
@property (nonatomic, weak) IBOutlet UIView *view;
@property (nonatomic, copy) ProductTappedBlock widgetTappedBlock;
- (id)initWithFrame:(CGRect)frame andFoo:(Foo *)foo;
@end
Edit:PS In your example, the error is caused because the owner sent to loadNib is 'self', which is your view controller. Your view controller doesn't have the 'myLabel' property, your custom view does.
编辑:PS 在你的例子中,错误是因为发送给 loadNib 的所有者是“self”,这是你的视图控制器。您的视图控制器没有 'myLabel' 属性,您的自定义视图有。
回答by zero3nna
I think you have to initiate them befor adding to your rootView!
我认为您必须在添加到您的 rootView 之前启动它们!
TestMyView *cell = [[TestMyView alloc]initWithNibName:@"TestMyView" bundle:[NSBundle mainBundle]];
[self.view addSubview: cell];