1

我有一个等于我想在 UIAlertView 中插入的文本字段的字符串。

NSString *finalScore = [[NSString alloc] initWithFormat:[counter2 text]];

UIAlertView *myAlertView = [[UIAlertView alloc]
                                initWithTitle:finalScore
                                message:@"You scored X points"
                                delegate:self
                                cancelButtonTitle:nil
                                otherButtonTitles:@"OK", nil];

[myAlertView show];
[myAlertView release];

我想用字符串“finalScore”中的分数替换“X”

我知道如何只使用分数,您只需在消息部分下输入不带引号的字符串名称,但我也希望那里的文本与字符串一起。

非常感谢,蔡斯

4

3 回答 3

4

将您的message:参数替换为:

message:[NSString stringWithFormat:@"You scored %@ points", finalScore]

另外,请注意,您的原始代码finalScore使用不带任何参数的字符串格式,这是无操作或不安全的(如果它包含%符号)。你也在泄露你的finalScore对象。

于 2009-09-16T20:30:20.763 回答
1

尝试以下类似的操作,并将其作为message变量传递给 UIAlertView 的初始化:

NSString* message = [NSString stringWithFormat:@"You scored %@ points",
                              [counter2 text]];

但是,您可能必须根据来自 的类型更改有关格式化字符串的一些详细信息[counter2 text],尽管上述方法应该适用于您将遇到的许多情况。

于 2009-09-16T20:32:00.073 回答
0

代替:

@"You scored X points"

利用:

[NSString stringWithFormat:@"You scored %@ points", finalScore]
于 2009-09-16T20:30:10.360 回答