将自定义子视图(在 xib 中创建)添加到视图控制器的视图 - 我做错了什么
我在 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
设置为 MyCustomView 并将 IBOutlet 连接到文件的所有者
在 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];
}
在 IB 中,我将文件的所有者和视图标识 我运行应用程序,添加了视图,但看不到标签、进度条和活动指示器。
我做错了什么?
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:
#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
In MyViewController.m, I've:
- (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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
-loadNibNamed
方法加载它。-initWithNibName
仅适用于 UIViewController。将以下代码添加到您的 MyCustomView init 方法中:
记住,如果您从笔尖初始化一个对象,它会调用
- (id)initWithCoder:(NSCoder *)aDecoder
进行初始化,因此您将如果您要在笔尖中创建 MyCustomView 对象,则可以覆盖它。如果您只是使用initWithFrame:
执行此操作,则只需覆盖它并添加上面的代码即可。另外,在你的 nib 中,确保你有一个顶级 UIView,并将所有其他元素放置在其中(这确保你的 subviewArray 只有一个条目)。这将从笔尖加载视图并将它们添加到对象中,并且应该可以解决问题。
You need to load it using the
-loadNibNamed
method.-initWithNibName
is only for UIViewControllers.Add the following code to your MyCustomView init method:
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 withinitWithFrame:
, 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).This will load the views from the nib and add them to the object, and should do the trick.
我认为你需要使用这种方法:
这是因为你需要将“nibNameOrNil”中的.xib文件名传递给它。
I think you need to use this method:
This is because you need to pass it the .xib filename in the "nibNameOrNil".
正如霍克所说,您必须使用该方法传递 XIB 名称(不带扩展名)。
然后你必须控制这个列表:
希望它有效。干杯。
As hocker said you have to use that method passing in the XIB name (without the extension).
Then you have to control this list:
Hope it works. Cheers.