Iphone 子类UIToolbar的正确方法

Iphone 子类UIToolbar的正确方法,iphone,objective-c,uinavigationcontroller,uitoolbar,Iphone,Objective C,Uinavigationcontroller,Uitoolbar,这是我第一次将UIToolbar子类化,以创建带有自定义UIBarButton的UIToolbar 我正在这样做: @interface CustomToolbar : UIToolbar @end @implementation CustomToolbar - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // add buttons

这是我第一次将UIToolbar子类化,以创建带有自定义UIBarButton的UIToolbar

我正在这样做:

@interface CustomToolbar : UIToolbar

@end


@implementation CustomToolbar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // add buttons
        UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(pressSignupButton:)];

        // add buttons to the array
        NSArray *items = [NSArray arrayWithObjects:myButton, nil];

        [self setItems:items];
    }
    return self;
}

@end
然后在我的视图控制器中:

CustomToolbar *myToolbar = [[CustomToolbar alloc] init];
[self.navigationController.view addSubview:myToolbar];
问题是我可以看到工具栏,但没有按钮。为什么?

注意:我更喜欢在没有nib的情况下以编程方式实现所有功能。

这行吗

CustomToolbar *myToolbar = [[CustomToolbar alloc] 
     initWithFrame:CGRectMake(0,0,self.navigationController.view.frame.size.width, 44)];
[self.navigationController.view addSubview:myToolbar];

您重写了-(id)initWithFrame:(CGRect)framem,但调用了init方法。之所以调用该方法,是因为它打印了专门用于此目的的
NSLog