使用自定义NSString更改按钮上的标题

时间:2015-06-05 14:11:40

标签: ios objective-c uibutton

我需要更改按钮的标题,但是按钮的标题需要根据我选择的NSArray的索引进行更改。我该怎么做?

代码:

   NSArray * answerChoiceArray1 = @[@"1492", @"13", @"Spain", @"1678", @"1775", @"1848", @"1899", @"1908", @"1928", @"1999", @"12,000s BCE", @"Caral", @"Caral", @"Nazca", @"France and Germany", @"Netherlands", @"1635", @"1883", @"1498", @"1917", @"", @"", @"", @"", @"", @"", @"", @"", @"", @""];
    NSArray * answerChoiceArray2 = @[@"1675", @"15", @"France", @"1733", @"1809", @"1910", @"1908", @"1929", @"1935", @"2001", @"8,000s BCE", @"Chibchas", @"Chavin", @"Wari", @"Austria and Italy", @"Spain", @"1789", @"1912", @"1532", @"1906", @"", @"", @"", @"", @"", @"", @"", @"", @"", @""];
    NSArray * answerChoiceArray3 = @[@"1776", @"27", @"Austria", @"1789", @"1852", @"1920", @"1917", @"1933", @"1939", @"2003", @"6,000s BCE", @"Canaris", @"Inca", @"Inca", @"Spain and Portugal", @"France", @"1822", @"1966", @"1617", @"1816", @"", @"", @"", @"", @"", @"", @"", @"", @"", @""];
    NSArray * answerChoiceArray4 = @[@"1783", @"50", @"Italy", @"1806", @"1861", @"1939", @"1921", @"1939", @"1941", @"2005", @"4,000s BCE", @"Chavin", @"Moche", @"Moche", @"Sweden and Germany", @"Portugal", @"1912", @"1973”", @"1769", @"1888", @"", @"", @"", @"", @"", @"", @"", @"", @"", @""];


    currentQuestion.answerChoice1 = [answerChoiceArray1 objectAtIndex:_generateQuestionArrayIndex];

    currentQuestion.answerChoice2 = [answerChoiceArray2 objectAtIndex:_generateQuestionArrayIndex];

    currentQuestion.answerChoice3 = [answerChoiceArray3 objectAtIndex:_generateQuestionArrayIndex];

    currentQuestion.answerChoice4 = [answerChoiceArray4 objectAtIndex:_generateQuestionArrayIndex];


    [_displayAnswerChoice1 setTitle:@"%@", currentQuestion.answerChoice1 forState:UIControlStateNormal];

2 个答案:

答案 0 :(得分:1)

只需使用变量名称(如果是字符串)

//如果是字符串

[_displayAnswerChoice1 setTitle : currentQuestion.answerChoice1 forState : UIControlStateNormal];

//如果要添加前缀,后缀或任何其他格式化文本,请使用stringWithFormat

[_displayAnswerChoice1 setTitle : [NSString stringWithFormat : @"%@", currentQuestion.answerChoice1] forState : UIControlStateNormal];

答案 1 :(得分:1)

如果您只需要直接设置值,那么您不需要@“%@”。

[_displayAnswerChoice1 setTitle:currentQuestion.answerChoice1
                       forState:UIControlStateNormal];