将UIAlertView文本转换为UILabel

时间:2015-08-04 14:59:19

标签: ios uilabel uialertview

有点奇怪的问题,我知道,但我想知道如何将信息从UIAlertView传输到UILabel。在过去的几个小时里,我一直在尝试自己没有运气。我正在使用一些预先存在的代码,因此我很难回过头来了解他们如何填充UIAlertView,但我认为它会更容易在标签上显示正文文本而不是键入代码。它还使用REST API来生成数据(根据政治对齐生成4个不同的实例)。

以下是UIAlertViewCode的片段

NSData *data = [NSJSONSerialization dataWithJSONObject:result options:0 error:0];

[[[UIAlertView alloc] initWithTitle:@"Response" message:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] 
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

我还需要其他任何代码吗?

提前感谢:)

1 个答案:

答案 0 :(得分:0)

将消息存储在变量中,然后重复使用它:

//message
NSData *data = [NSJSONSerialization dataWithJSONObject:result options:0 error:0];
NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

//UIAlert
[[[UIAlertView alloc] initWithTitle:@"Response" message:message 
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

//UILabel
yourLabel.text = message;