子类化 UIToolbar 时崩溃

发布于 11-16 04:08 字数 3024 浏览 2 评论 0原文

我对 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)]; 调用

我已经尝试了一切我能想到但仍然遇到同样的错误。我确信我只是没有保留一些将按钮指向错误位置的东西,但任何帮助将不胜感激 谢谢 达尔克

I have subclassed the UIToolbar to make it easier to implement a safari next, previous, done sort of thing.
It all worked fine when i was adding it directly (ie not subclassing) but now that i am, it crashes every time i click one of the buttons with
-[keyboardToolBar hideKeyboard:]: unrecognized selector sent to instance
This is the first time i have attempted to subclass something so im not sure if i have done something the wrong way.

Code for the subclass

@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

and is called using UIToolbar *keyboard = [[keyboardToolBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];

I have tried everything i can think of but still get the same error. Im sure i am just not retaining something somewhere of pointing the buttons to the wrong place but any help would be muchly appreciated
Thanks
Darc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里2024-11-23 04:08:11

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

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

这意味着必须有一个方法 hideKeyboard:(id)sender。但是您要么

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

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

The problem here is that your buttons are calling functions that take an input, but your functions do not take an input:

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

This means that there must be a method hideKeyboard:(id)sender. But you have

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

Either add the input to the function or remove the : from the selector call.

生死何惧2024-11-23 04:08:11

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

-(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 * 键盘的引用,因为你想要那个做自我。
实际上可能根本没有理由保留对按钮的引用,除非您稍后需要更改它们的标题或操作/目标对。

you effectively wrote it as a class method, here is a little bit better version, because of memory management issues etc.

-(UIToolbar*)initWithFrame:(CGRect)frame{
//Create a new toolbar

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;
}

also fix your methods to be either
@selector(someAction) to match -(void)someAction;
or @selector(someAction:) to match -(void)someAction:(id)sender;

also no reason to keep a reference to UIToolBar * keyboard, because you want that to be self.
actually probably no reason to keep references to the buttons at all unless you will need to change their titles or action/target pairs later.

兮子2024-11-23 04:08:11

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

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

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

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

You (or something you're doing) is calling hideKeyboard: (note the colon, indicating that the method has one argument.). However, the hideKeyboard method you've implemented, doesn't take any arguments.

Most likely, hideKeyboard should be changed to:

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

As an aside, the usual style for class names is capitalized, so your subclass should be KeyboardToolBar (and I'd also recommend keeping the same camel case as Apple's classes, so KeyboardToolbar would be best).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文