iPhone - 以正确的方式子类化 UIToolbar?
- 我创建了一个名为 CustomToolbar
- 然后我创建了一个空笔尖,添加了一个 工具栏,并设置工具栏 类为“CustomToolbar”。
在代码中初始化 CustomToolbar 以便我的类使用 nib 文件的正确方法是什么?
我已经编写了代码来执行此操作,但我知道这不是正确的方法并且存在泄漏。
@interface CustomToolbar : UIToolbar {
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *button;
@end
@implementation CustomToolbar
- (id)initWithDelegate
{
NSArray *objects = [[NSBundle mainBundle]
loadNibNamed:@"CustomToolbar"
owner:nil
options:nil];
if (self = (CustomToolbar*) [objects objectAtIndex:0])
{
//do some work here
}
return self;
}
@end
- I created a new Class named
CustomToolbar - Then i created an empty nib, added a
toolbar to it, and set the toolbar
class to "CustomToolbar".
What is the proper way of initializing CustomToolbar In code so that my class uses the nib file?
I already have written the code to do that but i know it's not the correct way and it has a leak.
@interface CustomToolbar : UIToolbar {
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *button;
@end
@implementation CustomToolbar
- (id)initWithDelegate
{
NSArray *objects = [[NSBundle mainBundle]
loadNibNamed:@"CustomToolbar"
owner:nil
options:nil];
if (self = (CustomToolbar*) [objects objectAtIndex:0])
{
//do some work here
}
return self;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NIB 将在您的自定义类上调用
initWithCoder:
,如下所示:如果您确实想按照现在的方式加载它,则需要
保留
从 <代码>loadNibNamed。The NIB will call
initWithCoder:
on your custom class, like this:If you really want to load it the way you do now, you need to
retain
the object returned fromloadNibNamed
.