使用填充了字母的UiButton来制作单词

时间:2013-09-05 23:16:49

标签: ios objective-c uibutton

我正在创建一个小游戏,界面与4图片1字非常相似。目前的设置如下: 用户获得随机生成的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];
}

还有第二个按钮区域,但这些区域是静态的,构成“单词”。当选择12个字母中的一个时,它将获取所选按钮的标签并填充相应的“单词”按钮之一。

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

}

Before Selection

After Selection

右键!所以这是我的问题。如果用户决定他们选择了错误的字母并希望删除该字母,他们可以按下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;
        }
    }
}
}
}

像魅力一样!

2 个答案:

答案 0 :(得分:1)

看起来这样做的一种方法是制作一个有7个空格的NSArray,每个空格都填充了源按钮的标记或-1。如果用户点击-1,则不执行任何操作。如果是数字,请将标记的项目设置为可见。

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

答案 1 :(得分:1)

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

解决方案1:

将12个按钮(底部)添加到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个元素的数组,您可以在其中匹配&#34; word&#34;带有字母按钮标签的按钮。像这样:

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

希望这有帮助!