iOS 5 中是否有标准方式来显示此屏幕截图中的删除确认?
如果没有标准的方法,任何解决方案都是好的。
这是一个UIActionSheet
具有破坏性的按钮集。请参阅文档。
例如
// 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
}
}
您可以使用 UIActionSheet 如下。
UIActionSheet *actionSheet = [UIActionSheet alloc] initWithTitle:@"Confirm" delegate:aDelegate cancelButtonTitle:nil destructiveButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[actionSheet showFromRect:aRect inView:aView animated:YES];
UIActionSheet
在 iOS 8 中已弃用。而是使用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 上的内置应用程序(如联系人、消息、邮件等)通常不使用这些字段。按钮不言自明。
请参阅该 UI 组件的文档:UIActionSheet和UIActionSheetDelegate