将NSString传递给UIAlertview的消息内容

时间:2014-07-05 13:01:11

标签: ios objective-c

我想在SampleUser poked you. UIAlertView中显示如下内容:message,但实际上我遇到了错误。我知道怎么用一个简单的字符串来做,我不知道怎么用包含另一个字符串的字符串来做。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Poke" message:@"%@ poked you.", self.senderString delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alertView show];

3 个答案:

答案 0 :(得分:4)

您应首先创建您的作品 - NSString,然后在UIAlertView中调用它:

NSString *message = [NSString stringWithFormat:@"%@ poked you.", userName];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Poke" message:message, self.senderString delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alertView show]; 

答案 1 :(得分:2)

这里的问题是,对于message:参数,您尝试发送此信息:

@"%@ poked you.", userName

这没有任何意义。

相反,您需要发送一个NSString对象作为参数。

NSString *message = [NSString stringWithFormat:@"%@ poked you.", self.senderString];

现在我们已经创建了一个NSString对象,我们可以使用这个对象作为消息参数。

您可以创建嵌入在调用中的此对象来创建警报视图,但为了便于阅读和调试,最好这样做。

NSString *message = [NSString stringWithFormat:@"%@ poked you.", self.senderString];
UIAlertView *pokeAlert = [[UIAlertView alloc] initWithTitle:@"Poke" 
                                                    message:message 
                                                   delegate:self
                                          cancelButtonTitle:@"Yes" 
                                          otherButtonTitles:@"No", nil];
[pokeAlert show];

答案 2 :(得分:1)

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Poke" message:[NSString stringWithFormat:@"%@ poked you.", self.senderString] delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alertView show];