在iOS 5中是否有标准的方法来执行删除确认,如联系人?

时间:2012-05-28 16:18:12

标签: iphone ios cocoa-touch

在iOS 5中是否有标准方式显示删除确认,如此截图中所示?

如果没有标准方法,任何解决方案都会很好。

delete confirmation

4 个答案:

答案 0 :(得分:12)

这是一个带有破坏性按钮的UIActionSheet。请参阅the documentation

e.g。

// Create the action sheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                   delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                     destructiveButtonTitle:@"Delete" 
                                         otherButtonTitles:nil];

...

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.destructiveButtonIndex) {
        // Do the delete
    }
}

答案 1 :(得分:2)

您可以使用UIActionSheet,如下所示。

UIActionSheet *actionSheet = [UIActionSheet alloc] initWithTitle:@"Confirm" delegate:aDelegate cancelButtonTitle:nil destructiveButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[actionSheet showFromRect:aRect inView:aView animated:YES];

答案 2 :(得分:1)

在iOS 8中不推荐使用

UIActionSheet。请使用UIAlertController,首选样式为UIAlertControllerStyleActionSheet

// Create a new alert, with an (optional) title and message
UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                               message:nil
                                                        preferredStyle:UIAlertControllerStyleActionSheet];


// When style is set to UIAlertActionStyleDestructive, the button appears with red text
UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"Delete"
                                                       style:UIAlertActionStyleDestructive
                                                     handler:^(UIAlertAction * action) {                                     
                                                          // ========
                                                          // Perform your delete operation here...
                                                          // ========
                                                      }];


// This cancel action will appear separated from the rest of the items
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction * action) {}];


// Add the actions to the alert
[alert addAction:deleteAction];
[alert addAction:cancelAction];

// Present the sheet.
[self presentViewController:alert animated:YES completion:nil];

我通常会省略删除确认的标题和消息字段,因为我发现iOS上的内置应用程序(如联系人,消息,邮件等)通常不会使用这些字段。按钮说明了一切。

答案 3 :(得分:0)

请参阅该UI组件的文档:UIActionSheetUIActionSheetDelegate