uialertview snippets

How to ask a user to confirm an action with UIAlertView and UIAlertSheet

Tagged uialertview, uialertsheet, prompt  Languages objectivec

UIAlertView

-(void) confirmDelete {
    UIAlertView *alertView = [[UIAlertView alloc]
            initWithTitle:@"Remove account" 
            message:@"Are you sure you want to remove this account?"
            delegate:self
            cancelButtonTitle:@"Remove"
            otherButtonTitles:nil];
    
    [alertView addButtonWithTitle:@"Don't remove"];
    [alertView show];
    [alertView release];
}

- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    [appDelegate.managedObjectContext deleteObject:selectedAccount];
    if (buttonIndex == 0) {
        NSLog(@"Remove button clicked");
    } else if (buttonIndex == 1) {
        NSLog(@"Cancel button clicked");
    }
}

UIAlertSheet

-(void)confirmDelete {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Remove account?" delegate:self cancelButtonTitle:@"Don't remove" destructiveButtonTitle:@"Remove account" otherButtonTitles:nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;   
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];   
    [actionSheet release];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"Remove button clicked");
    } else if (buttonIndex == 1) {
        NSLog(@"Cancel button clicked");
    }
}