无法将变量传递到警报视图中

时间:2014-09-03 16:41:11

标签: ios objective-c uialertview

我有一系列姓名'姓名' 。我试图在用户点击表格单元格时显示警报。如果用户触摸第一个单元格,那么[names objectatindex:0]应该是警报视图中的消息。这就是我所做的

 UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:@"ok" message:@"You have just tapped [names objectatindex:0]" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];

    okAlert.tag=0;


    [okAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

在消息中,它不显示名称。我无法弄清楚问题所在。你能帮帮我吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

试试这个:

UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:@"OK"
                                                  message:[NSString stringWithFormat:@"You have just tapped %@.", [names objectAtIndex:0]]
                                                 delegate:self
                                        cancelButtonTitle:@"No"
                                         otherButtonTitles:@"Yes", nil];

答案 1 :(得分:0)

message:@"You have just tapped [names objectatindex:0]"

您已将该代码隐藏在字符串常量中。现在它只是字母。

尝试:

UIAlertView *okAlert = [[UIAlertView alloc] initWithTitle:@"ok" 
  message:[@"You have just tapped " stringByAppendingString:names.firstObject]
  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];