gpt4 book ai didi

ios - 处理 UIButtons iOS

转载 作者:行者123 更新时间:2023-11-28 20:22:59 25 4
gpt4 key购买 nike

我一直在观看 2013 年冬季的 iOS 讲座(由 Paul Hegerty 主讲),我似乎无法理解为什么第二行代码在 Matchisimo 程序中是必需的。如果我将其注释掉,程序就会崩溃,但如果我保留它,它会正常工作。

[cardButton setTitle:card.contents forState:UIControlStateSelected];

[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];

在这一行失败:

@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CardGameAppDelegate class]));
}

第二行被注释掉会报错:

'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 47 beyond bounds [0 .. 46]'

卡片内容:

@property (strong, nonatomic) NSString *contents;

更新用户界面:

- (void)updateUI
{
for (UIButton *cardButton in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];

[cardButton setTitle:card.contents forState:UIControlStateSelected];

[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];

cardButton.selected = card.isFaceUp;
cardButton.enabled = !card.isUnPlayable;
}
}

最佳答案

注释此行 [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled]; 不会因任何原因导致崩溃。

这是行 [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]]; 导致与索引越界相关的崩溃。基本上你拥有的 cardButtons 比 self.game 拥有的卡片更多。

您可以用它包装它以防止崩溃,但应该寻找更深层次的潜在问题,了解为什么要创建额外的按钮。

int buttonIndex = [self.cardButtons indexOfObject:cardButton];
if (self.game.count > buttonIndex) {
Card *card = [self.game cardAtIndex:buttonIndex];

[cardButton setTitle:card.contents forState:UIControlStateSelected];

[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];

cardButton.selected = card.isFaceUp;
cardButton.enabled = !card.isUnPlayable;
}

关于ios - 处理 UIButtons iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229416/

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