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

'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

最佳答案

将按钮类型更改为UIButtonTypeCustom

CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom];

关于objective-c - 分配字符串时发送到实例的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13285712/

10-09 02:21