点击表格行时显示警报视图

发布于 2024-11-19 16:30:56 字数 187 浏览 2 评论 0原文

我有一个包含字符串列表的表视图,如下所示:

String1
String2
String3
String4

我想将其中一个设置为默认值,即当用户点击“String3”时,应该弹出一个警报视图,询问他们是否想要将该项目设置为默认值。

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

I have a table view with a list of strings as follows:

String1
String2
String3
String4

I want to make one of these the default, i.e., when the user taps "String3", an alert view should pop up asking if they want to make that item the default.

How would I implement this alert view in my table view controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(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
   }
}

First you'll want to define an instance variable to keep track of the string that's currently selected. Something like this in your header file will be fine.

NSString *selectedString;

Next, in your tableView:didSelectRowAtIndexPath: delegate method create an alert view.

- (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];
}

To save the value after the user taps a button in the alert you should use the UIAlertView delegate.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex == 2)
   {
     //Do something with selectedString here
   }
}
蒲公英的约定 2024-11-26 16:30:56

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

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

也就是说,当单击 yes 按钮时,你是否想做某事。不管怎样,感谢您的快速教程,除了那个轻微的打字错误之外,它运行得很好。

I don't have enough reputation to comment here. But your if statement should be:

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

That is to say if you want to do something when the yes button is clicked. Anyways, thanks for the quick tutorial, and apart from that slight typo it worked perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文