2011-07-12 41 views
1

我有一个字符串列表的表视图如下警报视图:显示在表行被窃听

String1 
String2 
String3 
String4 

我想让其中的一个默认值,即用户点击时“ String3“,应该弹出一个警告视图,询问他们是否想让该项目成为默认值。

我该如何在我的表视图控制器中实现此警报视图?

回答

2

首先,您需要定义一个实例变量来跟踪当前选中的字符串。在你的头文件中这样的东西将会很好。

NSString *selectedString; 

接下来,在您的tableView:didSelectRowAtIndexPath方法:委托方法创建一个警报视图。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedString = [stringArray objectAtIndex:indexPath.row]; 

    NSString *title = [NSString stringWithFormat:@"Make %@ default?", selectedString]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alert show]; 
    [alert release]; 
} 

要在用户点击警报中的按钮后保存该值,您应该使用UIAlertView委托。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 2) 
    { 
    //Do something with selectedString here 
    } 
} 
+0

如果用户点击“是”,我会如何做到这一点? – Baub

+1

您需要使用UIAlertView代理。我刚刚更新了我的答案。 –

0

我没有足够的声望在这里评论。但你的if语句应该是:

if (buttonIndex == 1) 
    { 
    //Do something with selectedString here 
    } 

也就是说,如果你想在点击yes按钮时做某事。无论如何,感谢您的快速教程,除了轻微的错字,它完美的工作。