gpt4 book ai didi

ios - 设置从数组中选择的按钮的标题 - Xcode iOS

转载 作者:行者123 更新时间:2023-12-01 19:18:44 25 4
gpt4 key购买 nike

我有很多按钮,我想在加载 View 时给它们一个标题。
按钮的标题因月份的哪一天而异。因此,我创建了一个包含所有按钮名称的数组。我的代码如下:

我的界面:

@property (strong, nonatomic) IBOutlet UIButton *button1;
@property (strong, nonatomic) IBOutlet UIButton *button2;
@property (strong, nonatomic) IBOutlet UIButton *button3;
@property (strong, nonatomic) IBOutlet UIButton *button4;
@property (strong, nonatomic) IBOutlet UIButton *button5;
@property (strong, nonatomic) IBOutlet UIButton *button6;
@property (strong, nonatomic) IBOutlet UIButton *button7;
@property (strong, nonatomic) IBOutlet UIButton *button8;
@property (strong, nonatomic) IBOutlet UIButton *button9;
@property (strong, nonatomic) IBOutlet UIButton *button10;

我的实现:
@synthesize button1 = _button1;
@synthesize button2 = _button2;
@synthesize button3 = _button3;
@synthesize button4 = _button4;
@synthesize button5 = _button5;
@synthesize button6 = _button6;
@synthesize button7 = _button7;
@synthesize button8 = _button8;
@synthesize button9 = _button9;
@synthesize button10 = _button10;

NSArray *myArray = [NSArray arrayWithObjects: @"dummyButton", @"_button1", @"_button2", @"_button3", @"_button4", @"_button5", @"_button6", @"_button7", @"_button8", @"_button9", @"_button10", nil];

for (int i = varDefinedEarlier; i<=totalAmountOfNeededTitles; i++) {
NSString *theTitleSting = [NSString stringWithFormat:@"%i",i];
NSLog(@"arrayValue:%@", [myArray objectAtIndex:i]); //Works prints the button name (for example _button7)


[[myArray objectAtIndex:i] setTitle:theTitleSting forState:UIControlStateNormal];
//The line above is where the app crashes. Everything else works just fine.
}

提供给我的错误是“线程 1:信号 SIGABRT

除此之外,这就是日志所说的:

-[__NSCFConstantString setTitle:forState:]: unrecognized selector sent to instance 0x14a48 2012-07-10 22:57:49.649 Don't Break the Chain[10302:707] * * * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTitle:forState:]: unrecognized selector sent to instance 0x14a48' * * * First throw call stack: (0x3774788f 0x3544c259 0x3774aa9b 0x37749915 0x376a4650 0xdc2f 0x31454c8b 0x314611e9 0x31461059 0x31460f3f 0x3146070b 0x31460503 0x31454aff 0x314547d5 0x314cd903 0x31547627 0x37fb8933 0x3771ba33 0x3771b699 0x3771a26f 0x3769d4a5 0x3769d36d 0x33e5c439 0x31449cd5 0xb7cf 0xb774) terminate called throwing an exception(lldb)



我认为错误是我试图告诉应用程序女巫按钮它需要设置标题的方式,但我无法真正弄清楚如何让它接受我的数组的值并将其用作按钮名称。

我是 iPhone iOS 开发的新手,如果答案很简单,请多多包涵;)

最佳答案

您没有引用按钮实例,而是引用该数组中的一些字符串。现在在您的循环中,您尝试在那些不存在 NSString 实例的字符串上调用 setTitle:forState: 方法。

尝试以下操作:

NSArray *myArray = [NSArray arrayWithObjects:_button1, _button2, _button3, _button4, _button5, _button6, _button7, _button8, _button9, _button10, nil];

这将按预期将实际按钮实例放入您的数组中,并且您的代码应该可以正常工作。

关于ios - 设置从数组中选择的按钮的标题 - Xcode iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11421990/

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