繁体   English   中英

将标签的内容添加到按钮的内容

[英]Adding contents of a label to contents of a button

我有一个数组questionHalf和一个next按钮,该按钮调用一个nextQuestionButton方法,该方法循环遍历questionHalf数组中的项目并在标签questionLabel中显示下一个字符串

questionHalf = @[@"Atlanta", @"Detroit", @"Houston", @"Green Bay", @"San Francisco"];

可以使用下一个按钮循环浏览:

 - (IBAction)nextQuestionButton:(id)sender;
    - (IBAction)nextQuestionButton:(id)sender
    {
        if (currentQuestionIndex < [questionHalf count] -1)
            currentQuestionIndex ++;
        else currentQuestionIndex = 0;
        _questionLabel.text = questionHalf [currentQuestionIndex];
    }

然后显示在标签中

@property (weak, nonatomic) IBOutlet UILabel *questionLabel;

我现在想再添加5个按钮,使用户可以从字符串中进行选择以完成美式足球队的名称。

当用户选择时,我需要查看组合的字符串是否为有效的团队名称。

我还有另一个数组correctFullAnswer,其中包含所有有效的完整团队名称,并且需要测试用户答案是否匹配此字符串中的答案之一。

correctFullAnswer = @[@"Atlanta Falcons", @"Detroit Lions", @"Houston Texans", @"Green Bay Packers", @"San Francisco 49'ers"];  

我还添加了5个必需按钮中的第一个

- (IBAction)packersAnswerButton:(id)sender;




- (IBAction)packersAnswerButton:(id)sender
    {
        NSString* selectedTeamName = [NSString stringWithFormat:@"%@ %@", questionHalf [currentQuestionIndex], answerHalf];
        if ([correctFullAnswer indexOfObject:selectedTeamName] != NSNotFound)
        {
            [UIView animateWithDuration:0.5 animations:^{
                [packersAnswerButton.alpha setAlpha:1];
            }
        ];}

        }

但是,当我选择正确答案后,试图隐藏按钮时,我得到了未声明标识符'packersAnswerButton'的错误。

只需在要添加按钮的视图中添加5个按钮即可。 该过程类似于以下内容:

-(void)viewDidLoad {
    //i'm assuming containerView is the view where you are going to add the buttons
    NSArray* teamNameSuffices = @[@"Falcons", @"Lions", @"Texans", @"Packers", @"49'ers"];
    int i = 0;
    for(NSString* suffix in teamNameSuffices) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(<%# your required frame coordinates #>);
        btn.titleLabel.text = suffix;
        btn.tag = i;
        [btn addTarget:self action:@selector(suffixSelected:) forControlEvents:UIControlEventTouchUpInside];
        [containerView addSubview:btn];
        i++;
    }
}

-(void)suffixSelected:(UIButton*)sender {
    //here you retrieve suffix text and compare if it is valid team name
    NSString* suffix = sender.titleLabel.text;
    NSString* selectedTeamName = [NSString stringWithFormat:@"%@ %@",questionHalf [currentQuestionIndex]/* or _questionLabel.text, whichever is accessible */,suffix];
    if([correctFullAnswer indexOfObject:selectedTeamName] != NSNotFound) { //a valid team name is constructed
        [[[[UIAlertView alloc] initWithTitle:@"Congratulations!" message:@"Bingo!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease] show];
        //or do whatever is required
    }
}

(可选)您还可以从设计器(storyboard / xib)创建后缀按钮,相应地设置其标题,并直接从其将目标设置为suffixSelected:方法。 在这种情况下,您需要将方法声明为(IBAction)而不是(void)

如果您需要探索其他任何东西,请告诉我。

可能是您遇到的错误。

通过外观,您已经创建了按钮动作。 (IBAction)并将按钮连接到动作。

但可能尚未将按钮链接到IBoutlet。

完成后,您将使用它来设置按钮属性。

@property IBOutlet (nonatomic) UIButton  packersButton;

看看这个Apple Doc Interface Builder帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM