0

我有一个自定义按钮,我想分配一个字符串,然后将该按钮作为@selector 中的参数传递。出于这个原因,在头文件 CustomButton 中,我放置了一个属性“名称”。但是,当我将字符串分配给按钮时,出现此错误。

'NSInvalidArgumentException', reason: '-[UIButton setName:]: unrecognized selector sent 
to instance 0x8484560'

这是什么意思?

CustomButton.h 中的代码:

 @interface CustomButton : UIButton

 @property (nonatomic, strong)NSString * name;

 @end

CustomButton.m 中的代码:

 @synthesize name;

在 MapViewController.m

CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeInfoLight];

NSLog(@"The button is %@", rightButton);
/*The button is <UIButton: 0x8484560; frame = (0 0; 18 19); opaque = NO; layer = 
  <CALayer: 0x8484670>>*/

rightButton.name = self.nameTable; //the error is here
4

4 回答 4

2

所以,我有一个类似的“自定义按钮”,它似乎工作正常:

CustomButton* btn = [CustomButton buttonWithType:UIButtonTypeCustom];
[btn setName:@"Road Runner"];
//OR 
btn.name = @"Road Runner";

NSLog(@"Custom button %@", btn);
/*
OUTPUT: <CustomButton: 0x68283f0; baseClass = UIButton; frame = (0 0; 0 0); 
opaque = NO; layer = <CALayer: 0x6828500>>
*/

NSLog(@"Button's name is %@", btn.name);
/* 
OUTPUT: Road Runner //the name that was set earlier
*/

因此,请检查您是否正确创建了CustomButton该类,因为在控制台中,它是正在显示的 UIButton。

于 2012-11-08T09:36:24.523 回答
1

你是如何设置标题的?你还没有发布代码。应该是这样的——

[rightButton setTitle:[NSString stringWithFormat:@"%@", buttonTitle]  forState:UIControlStateNormal];
于 2012-11-08T09:11:54.553 回答
1

将按钮类型更改为UIButtonTypeCustom

CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom];
于 2012-11-08T09:18:45.510 回答
0

没有 setName :D setTitle forState (如 sriskar 所说)或 button.titleLabel.text IIRC

于 2012-11-08T09:21:41.207 回答