0

我正在创建一个界面非常类似于 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;
        }
    }
}
}
}

奇迹般有效!

4

2 回答 2

1

似乎一种方法是制作一个有 7 个空格的 NSArray,每个空格都填充源按钮的标记或 -1。如果用户点击 -1,则什么也不做。如果是数字,则将该标记项设置为可见。

此外,您的代码非常不干燥(不要重复自己) - 您应该花一些时间考虑编程以及如何更好地使用数据结构。如果您需要更明确的建议,请在评论中提问,我会进行编辑。:)

于 2013-09-05T23:50:23.013 回答
1

我可以想到几种方法来实现这一点。

解决方案1:

将 12 个按钮(底部)添加到 an 中,NSArray因此当用户按下 7 个按钮之一(顶部)时,您可以遍历该数组并比较标题。像这样的东西:

for (UIButton *button in buttonsArray) {
    NSString *buttonTitle = [button titleForState:UIControlStateNormal];
    // I'm assuming here that you already have the touched button tile
    if ([buttonTitle isEqualToString:touchedButtonTitle]) { 
        button.hidden = NO;
    }
}

解决方案2:

创建一个包含 7 个元素的数组,您可以在其中将“单词”按钮与字母按钮的标签进行匹配。像这样的东西:

-(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];

        // you know this is object One. Insert tag at position zero (arrays are zero-based)
        self.topButtonsArray insertObject:[NSNumber numberWithInt:sender.tag] atIndex:0];
    }
.
.
.

希望这可以帮助!

于 2013-09-05T23:50:30.573 回答