2014-07-05 62 views
0

我想在UIAlertViewmessage中显示如下内容:SampleUser poked you.,但实际上我收到错误。我知道如何用一个简单的字符串做到这一点,我不知道如何用一个包含另一个字符串的字符串来做到这一点。将NSString传递到UIAlertview的邮件内容

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

“但实际上我得到的错误” - 我们都非常抱歉听到。 –

+0

@HotLicks我认为在这种情况下,将错误命名并不重要,因为问题很明显。 – rihe

+1

@ vv88虽然我同意问题很明显,但指定错误绝不应被视为无关紧要。如果提问者知道错误是无关紧要的,那么提问者应该能够轻松地回答他自己的问题,而不用发布到StackOverflow。当解决方案对您不明显时,您应该发布到StackOverflow,如果答案对您不明显,则应该包含错误消息。首先,因为您无法确定是否对其他人显而易见(因为这对您并不明显),对于两人来说也是如此,因为这会使错误更容易被发现。 – nhgrif

回答

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]; 
+0

这也是一个正常工作,正确的解决方案。我接受了另一个问题,因为它很快就发布了,但我也给+1,因为它完全是当之无愧的。谢谢。 – rihe

+0

这很好,但只是考虑其他答案并没有真正解释问题/解决方案,而且其他答案实际上并不完全正确,如果您只是复制并粘贴它,则无法正常工作。 – nhgrif

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

您应该创建composed- 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]; 
+0

@sebastien回答说最好把复合陈述分成几个单独的陈述。它更清晰,更易于调试,并且通常在最终生成的代码中效率更高。这将使您的编码生活更轻松。 – zaph

+0

你的信息:“论点是错误的。 – nhgrif

+0

这很好,谢谢。 – rihe