一个视图上的两个警报视图

Two Alertviews on one view


编辑:

问题已解决 ==> 只需给一个标签即可解决问题。


我有以下问题:

在一个视图上我有两个 UIalertview:

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'

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

是的,就像你说的,给一个标签可以让你在一个视图中有多个 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 .

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