我有一个自定义按钮,我想分配一个字符串,然后将该按钮作为@selector中的参数传递。出于这个原因,在头文件CustomButton中我放了一个属性“name”。但是,当我将字符串分配给按钮时,我收到此错误。
'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
答案 0 :(得分: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。
答案 1 :(得分:1)
你是如何设置标题的?你还没有发布代码。它必须是这样的 -
[rightButton setTitle:[NSString stringWithFormat:@"%@", buttonTitle] forState:UIControlStateNormal];
答案 2 :(得分:1)
将按钮类型更改为UIButtonTypeCustom
。
CustomButton *rightButton = [CustomButton buttonWithType:UIButtonTypeCustom];
答案 3 :(得分:0)
没有setName:D setTitle forState(如sriskar所说) 要么 button.titleLabel.text IIRC