gpt4 book ai didi

iOS 如何为来自 CollectionView 的图像上的 UIPreviewActions 确认 'Delete'

转载 作者:行者123 更新时间:2023-12-03 19:41:02 25 4
gpt4 key购买 nike

与 Apple 的照片应用类似,我在图像的 collectionView 上使用 peek 和 pop。当你查看一个单元格时,你会看到 PhotoViewController 上的图像,它只在 imageView 中显示该图像。 Apple 提供了 4 种不同的预览操作:复制、共享、收藏、删除。如果用户点击“删除”,预览操作将更新为:删除和取消。 peek 图像仍然显示。

在我自己的代码中,我可以设置预览操作并删除照片,但无法在删除之前确认删除。我也不知道如何使用删除或取消的新预览操作保持 peek View 打开(或重新打开)。

问题:如何让用户在删除之前确认删除?

我尝试将 UIAlertController 放入 UIPreviewAction 中,但收到警告:“尝试呈现其 View 不在窗口层次结构中!”我尝试了一些替代方法,例如使用 keyWindow 来呈现 alertController,但无法使其快速工作。我假设我可以为 Collection View 设置另一个通知,这样当 peek 以“删除”关闭时,会出现一个警报 Controller ,要求用户确认删除,但到那个阶段图像不再出现。

有什么建议吗?以下是我目前使用的替代方案。

UIPreviewAction *delete = [UIPreviewAction actionWithTitle:@"Delete" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController)
{

//create alertController
...

// 1. present alertController:
[self presentViewController:alertController animated:YES completion:nil];

// 2. Alternatively tried to present alertController using keyWindow
UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController;
[top presentViewController:alertController animated:YES completion: nil];

//3. notify the presenting view controller that the user has deleted the image and set up an alert to occur after the peek view closes.

[[NSNotificationCenter defaultCenter] postNotificationName:@"deleteAlert" object:nil];
}

最佳答案

我知道这是一个老问题,但解决方案很简单,所以在这里发布!

swift 3

        let closeTabAction = UIPreviewAction(title: "Close Tab", style: .destructive, handler: { (action, viewController) in
//this is confirmation action
})

let closeTabCancelAction = UIPreviewAction(title: "Cancel", style: .default, handler: { (action, viewController) in
//cancel action, don't need anything here
})


//UIPreviewActionGroup, this is the main action
let closeTabGroup = UIPreviewActionGroup(title: "Close Tab", style: .destructive, actions: [closeTabAction, closeTabCancelAction])

return [closeTabGroup]

基本上,您只需要使用 UIPreviewActionGroup 来创建子菜单。 Apple Docs

关于iOS 如何为来自 CollectionView 的图像上的 UIPreviewActions 确认 'Delete',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com