如果用户从UIAlertView中选择取消按钮,则停止从UITable删除用户

时间:2014-05-20 01:02:31

标签: ios objective-c uialertview

我有一个表从一个plist加载一些名称,我正在尝试做的基本上是删除用户选择的一个,一切都工作正常,但我正在努力做到这一点,我无法弄清楚基本上是给用户选择取消或使用UIAlertView继续删除。

这就是我所拥有的显然不起作用,无论触摸了什么按钮,它都会删除用户。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // This is fine since here I'm only notifying the user that guest cannot be deleted
    UITableViewCell *celectedRow = [self.tableScores cellForRowAtIndexPath:indexPath];
    NSString *removeUserWithKey = celectedRow.textLabel.text;

    if ([removeUserWithKey isEqual: @"Guest"])
    {
        // don't remove anything and show message
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
                                                        message:@"The user 'Guest' cannot be removed."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
        [alert show];

    }
    else
    {
        // the problem I have is here, how can I stop deleting and saving  
        // if user selects the cancel button
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
                                                        message:@"The user 'Guest' cannot be removed."
                                                       delegate:self
                                              cancelButtonTitle:@"Continue"
                                              otherButtonTitles:@"Cancel", nil];
        [alert show];

        //IF USER NAME IS OTHER THAN GUEST REMOVE IT
        // remove selected row from table
        [arrayRecords removeObjectAtIndex:indexPath.row];

        // remove selected user from dictionary
        [dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];

        // write dictionary to plist after removing items
        [self.pListReader writeToPlist:@"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];

        // reload items in table to reflect any changes made
        [self.tableScores reloadData];
    }
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Continue"])
    {
        NSLog(@"Continue.");
    }
    else if([title isEqualToString:@"Cancel"])
    {
       NSLog(@"Cancelled");
    }
}

同样,如果我不想让用户选择取消删除,此代码可以正常工作。

如果用户选择取消按钮,我如何以可以取消删除过程的方式构建代码?

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

只有当用户按下“继续”按钮时,才需要确保从阵列中删除对象。将用于删除guest虚拟机的代码移动到警报视图委托

使用行号标记警报视图。

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information!"
                                                    message:@"The user 'Guest' cannot be removed."
                                                   delegate:self
                                          cancelButtonTitle:@"Continue"
                                          otherButtonTitles:@"Cancel", nil];
    alert.tag = indexPath.row;
    [alert show];

删除警报视图委托中的记录。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

  if([title isEqualToString:@"Continue"])
  {
    NSLog(@"Continue.");

    //Remove the corresponding row.
    [arrayRecords removeObjectAtIndex:alertView.tag];

    // remove selected user from dictionary
    [dictionaryCopyOfRecordsFromPlist removeObjectForKey:removeUserWithKey];

    // write dictionary to plist after removing items
    [self.pListReader writeToPlist:@"studentsRecords.plist" withDictionary:dictionaryCopyOfRecordsFromPlist];

    // reload items in table to reflect any changes made
    [self.tableScores reloadData];
}
  else if([title isEqualToString:@"Cancel"])
  {
     NSLog(@"Cancelled");
     //Do Nothing
  }
}