gpt4 book ai didi

objective-c - 分配字符串时发送到实例的无法识别的选择器

转载 作者:行者123 更新时间:2023-11-28 19:14:13 26 4
gpt4 key购买 nike

我有一个自定义按钮,我想为其分配一个字符串,然后将该按钮作为参数传递到@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

最佳答案

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

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。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com