我已经将UIToolbar子类化,以便更容易实现下一步的safari,上一步,完成的事情。当我直接添加它(即不是子类化)时,它工作得很好,但现在我添加了,每次我用-[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
发布于 2011-06-18 07:29:13
您(或您正在做的事情)正在调用hideKeyboard: (注意冒号,表示该方法只有一个参数)。但是,您实现的hideKeyboard方法不接受任何参数。
最有可能的情况是,hideKeyboard应该更改为:
- (void)hideKeyboard:(id)sender {
NSLog(@"hello");
}顺便说一句,通常的类名样式是大写的,所以你的子类应该是KeyboardToolBar (我也建议保持和苹果的类一样的驼峰大小写,所以KeyboardToolbar是最好的)。
发布于 2011-06-18 07:31:50
这里的问题是,您的按钮正在调用接受输入的函数,但您的函数不接受输入:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(hideKeyboard:)];这意味着必须有一个方法hideKeyboard:(id)sender。但是你有
-(void)hideKeyboard{
NSLog(@"hello");
}将输入添加到函数或从选择器调用中删除:。
发布于 2011-06-18 07:32:43
你有效地把它写成了一个类方法,由于内存管理等问题,这里是一个更好的版本。
-(UIToolbar*)initWithFrame:(CGRect)frame{ //新建工具栏
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 *键盘的引用,因为您希望引用的是自己。实际上,可能根本没有理由保留对按钮的引用,除非您稍后需要更改它们的标题或操作/目标对。
https://stackoverflow.com/questions/6392753
复制相似问题