我正在创建一个界面非常类似于 4 Pictures 1 word 的小游戏。目前的设置如下: 用户得到一个随机生成的 7 个字母的单词。有一个数组接受 7 个字母,然后在 12 个以编程方式创建的 UIButtons 中随机填充其他字母:
for (int i=0; i< buttonCount; i++) {
int xpositiong = x + 37;
x = xpositiong;
isSeven ++;
restAt7 ++;
NSString *stringFromInt = [NSString stringWithFormat:@"%@",[arrayForRound objectAtIndex:i]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(buttonSelected:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:stringFromInt forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"helvetica" size:19];
button.tag = i+10;
_letterButton = button;
if (restAt7 == 7) {
x = 47;
}
if (isSeven <= 6) {
button.frame = CGRectMake(x,333,40,40);
}
else
{
button.frame = CGRectMake(x,370,40,40);
}
[self.view addSubview:button];
}
还有第二个按钮区域,但是它们是静态的并构成“单词”。When one of the 12 letters are selected it takes the tag of the selected button and populates one of the corresponding "word" buttons.
-(void)buttonSelected : (UIButton *)sender
{
NSLog(@"sender %i", sender.tag);
UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
button.hidden = YES;
if ([_buttonOne.titleLabel.text isEqualToString: @" "]) {
// NSLog(@"button is nil");
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
[_buttonOne setTag:button.tag];
}
else if ([_buttonTwo.titleLabel.text isEqualToString:@" "])
{
[_buttonTwo setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
else if ([_buttonThree.titleLabel.text isEqualToString:@" "])
{
[_buttonThree setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
else if ([_buttonFour.titleLabel.text isEqualToString:@" "])
{
[_buttonFour setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
else if ([_buttonFive.titleLabel.text isEqualToString:@" "])
{
[_buttonFive setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
else if ([_buttonSix.titleLabel.text isEqualToString:@" "])
{
[_buttonSix setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
else if ([_buttonSeven.titleLabel.text isEqualToString:@" "])
{
[_buttonSeven setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
}
正确的!所以这是我的问题。如果用户决定他们选择了错误的字母并希望删除该字母,他们可以按 7 个按钮之一来删除该字母。但是我不知道如何取消隐藏最初填充有字母的相应按钮......
我相信这与保留所选按钮的标签有关,但是因为 7 个按钮中的每一个都分配有标签,所以我不知道该怎么做。
编辑:
我将发布有效的解决方案。此外,所有功能都已用循环重写。
-(void)buttonSelected : (UIButton *)sender
{
NSLog(@"sender %i", sender.tag);
UIButton *button = (UIButton *)[self.view viewWithTag:sender.tag];
button.hidden = YES;
for (int i = 0; i < _wordButtonsArray.count; i ++) {
UIButton *selectedButton = [_wordButtonsArray objectAtIndex:i];
if ([selectedButton.titleLabel.text isEqualToString:@" "]) {
[selectedButton setTitle:sender.titleLabel.text forState:UIControlStateNormal];
[selectedButton setTag:button.tag];
return;
}
}
[self checkIfAnswerIsFinished:sender];
}
- (IBAction)clearButton:(id)sender
{
UIButton *button = (UIButton *)[self.view viewWithTag:[sender tag]];
button.hidden = NO;
for (int i = 0; i < _wordButtonsArray.count; i ++) {
if (button == [_wordButtonsArray objectAtIndex:i]) {
[button setTitle:@" " forState:UIControlStateNormal];
for (int letterButtonsIndex = 0; letterButtonsIndex < _letterButtonsArray.count; letterButtonsIndex++) {
UIButton *letterButton = [_letterButtonsArray objectAtIndex:letterButtonsIndex];
if (button.tag == letterButton.tag) {
button.tag = i;
letterButton.hidden = NO;
}
}
}
}
}
奇迹般有效!