子类化UIToolbar时崩溃

时间:2011-06-17 23:21:44

标签: iphone objective-c uitoolbar

我已经将UIToolbar子类化,以便更容易实现下一个,之前的,完成的事情。 当我直接添加它(即不是子类)时它一切正常工作但现在我,它每次我点击其中一个按钮时崩溃 -[keyboardToolBar hideKeyboard:]: unrecognized selector sent to instance 这是我第一次尝试子类化,所以我不确定我是否做错了。

子类的代码

@interface keyboardToolBar : UIToolbar {
UIToolbar *keyboard;
UIBarItem *previous;
UIBarItem *next;
UIBarItem *done;
}

@property (nonatomic, retain) UIToolbar *keyboard;
@property (nonatomic, retain) UIBarItem *previous;
@property (nonatomic, retain) UIBarItem *next;
@property (nonatomic, retain) UIBarItem *done;

-(void)previousField;
-(void)nextField;
-(void)hideKeyboard;


@end


#import "keyboardToolBar.h"


@implementation keyboardToolBar
@synthesize keyboard, previous, done, next;

-(UIToolbar*)initWithFrame:(CGRect)frame{
//Create a new toolbar
keyboard = [[UIToolbar alloc]initWithFrame:frame];

//Create all the buttons and point them to methods
UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" 
                                                            style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(previousField:)];
UIBarButtonItem *nextButton     = [[UIBarButtonItem alloc] initWithTitle:@"Next" 
                                                            style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(nextField:)];
UIBarButtonItem *filler     = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                            target:nil 
                                                            action:nil];
UIBarButtonItem *doneButton     = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                            style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(hideKeyboard:)];

//Set the width of both of the buttons to make it look pritty
previousButton.width = 70.0f;
nextButton.width = 70.0f;

self.previous = previousButton;
self.next = nextButton;
self.done = doneButton;

//Add the buttons to the toolbar
[keyboard setItems:[[[NSArray alloc] initWithObjects:self.previous, self.next, filler, self.done, nil] autorelease]];


//Release the buttons
[previous release];
[next release];
[filler release];
[done release];


//return the shiny new toolbar
return keyboard;
}

-(void)previousField{

}

-(void)nextField{

}

-(void)hideKeyboard{
NSLog(@"hello");
}


@end

并使用UIToolbar *keyboard = [[keyboardToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];

进行调用

我已经尝试了我能想到的一切,但仍然得到同样的错误。我确定我只是没有保留某些东西,将按钮指向错误的地方,但任何帮助将非常感激 谢谢 DARC

3 个答案:

答案 0 :(得分:1)

这里的问题是你的按钮是调用输入的函数,但是你的函数没有输入:

UIBarButtonItem *doneButton     = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                            style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(hideKeyboard:)];

这意味着必须有方法hideKeyboard:(id)sender。但你有

-(void)hideKeyboard{
NSLog(@"hello");
}

将输入添加到函数或从选择器调用中删除:

答案 1 :(得分:1)

你有效地把它写成了一个类方法,这里有一个更好的版本,因为内存管理问题等。

- (UIToolbar *)initWithFrame:方法(的CGRect)帧{     //创建一个新工具栏

if ((self = [super initWithFrame:frame]))


//Create all the buttons and point them to methods
previous = [[UIBarButtonItem alloc] initWithTitle:@"Previous"  style:UIBarButtonItemStyleBordered target:self  action:@selector(previousField:)];

next  = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered  target:self  action:@selector(nextField:)];
UIBarButtonItem *filler     = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  target:nil  action:nil];
done     = [[UIBarButtonItem alloc] initWithTitle:@"Done"  style:UIBarButtonItemStyleBordered target:self action:@selector(hideKeyboard:)];

//Set the width of both of the buttons to make it look pritty
previous.width = 70.0f;
next.width = 70.0f;

//Add the buttons to the toolbar
[keyboard setItems:[[[NSArray alloc] initWithObjects:previous, next, filler, done, nil] autorelease]];


//Release the buttons
[previous release];
[next release];
[filler release];
[done release];


//return the shiny new toolbar
return self;
}

也可以修复您的方法 @selector(someAction)匹配-(void)someAction;@selector(someAction:)匹配-(void)someAction:(id)sender;

也没有理由保留对UIToolBar *键盘的引用,因为你希望它是自己的。 实际上可能没有理由保留对按钮的引用,除非你以后需要更改它们的标题或动作/目标对。

答案 2 :(得分:0)

您(或您正在做的事情)正在调用hideKeyboard:(请注意冒号,表示该方法有一个参数。)。但是,您实现的hideKeyboard方法不会带任何参数。

最有可能的是,hideKeyboard应更改为:

- (void)hideKeyboard:(id)sender {
   NSLog(@"hello");
}

顺便说一下,类名的通常样式是大写的,所以你的子类应该是KeyboardToolBar(我还建议保留与Apple的类相同的驼峰案例,因此KeyboardToolbar将是最好的)。