Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 子类化UIToolbar时崩溃_Iphone_Objective C_Uitoolbar - Fatal编程技术网

Iphone 子类化UIToolbar时崩溃

Iphone 子类化UIToolbar时崩溃,iphone,objective-c,uitoolbar,Iphone,Objective C,Uitoolbar,我已经对UIToolbar进行了子类化,以便更容易地实现safari下一步、上一步、完成某种事情。 当我直接添加它时(即不是子类化)一切都很好,但现在我这样做了,每次我用鼠标点击其中一个按钮时,它都会崩溃 -[keyboardToolBar hideKeyboard:]:无法识别的选择器发送到实例 这是我第一次尝试子类化一些东西,所以我不确定我是否做了一些错误的方式 子类的代码 @interface keyboardToolBar : UIToolbar { UIToolbar *keyboar

我已经对UIToolbar进行了子类化,以便更容易地实现safari下一步、上一步、完成某种事情。 当我直接添加它时(即不是子类化)一切都很好,但现在我这样做了,每次我用鼠标点击其中一个按钮时,它都会崩溃
-[keyboardToolBar hideKeyboard:]:无法识别的选择器发送到实例
这是我第一次尝试子类化一些东西,所以我不确定我是否做了一些错误的方式

子类的代码

@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

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

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

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

另一方面,通常的类名样式是大写的,因此您的子类应该是
KeyboardToolBar
(我还建议保留与Apple类相同的驼峰大小写,因此
KeyboardToolBar
最好)。

这里的问题是,您的按钮正在调用接受输入的函数,但您的函数不接受输入:

UIBarButtonItem *doneButton     = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                            style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(hideKeyboard:)];
这意味着必须有一个方法
hideKeyboard:(id)sender
。但是你有

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

要么向函数添加输入,要么从选择器调用中删除

您有效地将其作为类方法编写,由于内存管理等问题,这里有一个更好的版本

-(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;
}
同时将您的方法修改为
@选择器(someAction)
匹配
-(void)someAction;
@选择器(someAction:)
匹配
-(void)someAction:(id)发送方;

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

Dam it,new it’s简单。谢谢:原始代码返回的是UIToolbar,而不是键盘工具栏(几乎肯定是错的)并泄漏正在接收EFENT的键盘工具栏!