一个视图上的两个Alertview

时间:2015-04-16 13:08:36

标签: objective-c uialertview


编辑:

解决问题==>简单地给出标签解决了问题。


我有以下问题:

在视图中,我有两个UIalertviews:

NSString *message = [NSString stringWithFormat:@"Users must enter this code to join the meeting: %@", meetingCode];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Meeting code"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:@"Copy to clipboard", nil];
    [alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
    NSLog(@"Code not copied");
}
else
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = meetingCode;
    NSLog(@"Code copied"); 
}
}

和这一个:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {

    AgendaModel* agenda = _meeting.agenda[indexPath.row] ;
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:agenda.id,@"id",agenda.name,@"name", nil];
    NSString *message = [NSString stringWithFormat:@"Are you sure that you want to delete : %@?", agenda.name];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning"
                                                    message:message
                                                   delegate:self
                                          cancelButtonTitle:@"Close"
                                          otherButtonTitles:@"Delete", nil];
    [alert show];

    NSString *delete_url = [NSString stringWithFormat:@"RestAgendas/delete.json"];
    [_meeting.agenda removeObject:agenda];
    [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [JSONAPI getWithPath:delete_url andParams:dict completion:^(id json, JSONModelError *err) {
        NSLog(@"%@", json);
    }];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
    NSLog(@"Agenda Won't Be Deleted");
}
else
{
    NSLog(@"Agenda Will Be Deleted");
}
}

现在是我收到错误的问题:方法'alertView:clickedButtonAtIndex'的重复声明

我该如何解决这个问题?我尝试了一些我在这里找到的东西,但我仍然无法使它工作。有人能帮我吗?

1 个答案:

答案 0 :(得分:0)

是的,就像你说的那样,给一个标签可以让你在一个视图中有多个UIAlertView

UIActionSheetUIAlertController

也是如此

以供将来参考,即使这可能是重复的,只需像往常一样创建警报并添加

myAlert.tag = 123; //any number of your choice

并在alertView:clickedButtonAtIndex

您可以使用switch或某些if's

找到它
if (alertview.tag = 123){

// this is my alert

}else if(alertview.tag = 333){

// this is my other alert

}

对于它的价值,我建议使用else if而不只是if来避免每次都读取整个方法,并通过降低被调用的可能性来命令if's。< / p>

请注意,您还可以为按钮,视图,单元格,标签以及几乎所有可以找到的插座提供标签。