首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 13.0 UIMenu和UIAction for UIContextMenuConfiguration

iOS 13.0 UIMenu和UIAction for UIContextMenuConfiguration
EN

Stack Overflow用户
提问于 2019-07-09 20:54:35
回答 3查看 9.4K关注 0票数 5

我正在尝试使用iOS 13.0Beta中引入的新API。我下载了Xcode 11.0Beta 3来访问这些API。

我在网上找到的一些代码做了如下工作:

代码语言:javascript
复制
extension SingleViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu<UIAction>? in
            // Creating Save button
            let save = UIAction(__title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill"), options: []) { action in
                // Just showing some alert
                self.showAlert(title: action.title)
            }

            // Creating Rotate button
            let rotate = UIAction(__title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), options: []) { action in
                self.showAlert(title: action.title)
            }
            // Creating Delete button
            let delete = UIAction(__title: "Delete", image: UIImage(systemName: "trash.fill"), options: .destructive) { action in
                self.showAlert(title: action.title)
            }
            // Creating Edit, which will open Submenu
            let edit = UIMenu<UIAction>.create(title: "Edit...", children: [rotate, delete])

            // Creating main context menu
            return UIMenu<UIAction>.create(title: "Menu", children: [save, edit])
        }
        return configuration
    }
}

这似乎很好,但在我的Xcode中甚至没有编译。我所犯的错误是:

Cannot specialize non-generic type 'UIMenu' Replace '<UIAction>' with ''

关于创建配置常量。

Type of expression is ambiguous without more context

关于创建保存操作。

还有其他几个类似的错误。

我还应该指出,我甚至没有这种格式的构造函数:

代码语言:javascript
复制
UIAction(__title: "String", image: UIImage, options: Array)
UIMenu.create(...)

为什么在Xcode-11 beta 3.0中缺少这些?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-07-09 21:42:04

嗯,它变了。这是个测试版!我希望它会再次改变,但是现在,在beta 3中,UIAction的初始化程序是

代码语言:javascript
复制
init(__title title: String, image: UIImage?, identifier: UIAction.Identifier?, 
    handler: @escaping UIActionHandler)

UIMenu不是泛型的,它的初始化程序是

代码语言:javascript
复制
init(__title title: String, image: UIImage?, identifier: UIMenu.Identifier?, 
    options: UIMenu.Options = [], children: [UIMenuElement])

下面是对在beta 3下编译的代码的重写:

代码语言:javascript
复制
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu? in
        let save = UIAction(__title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill"), identifier: nil) { action in
            // whatever
        }
        let rotate = UIAction(__title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise"), identifier: nil) { action in
            // whatever
        }
        let delete = UIAction(__title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil) { action in
            // whatever
        }
        let edit = UIMenu(__title: "Edit", image: nil, identifier: nil, children:[rotate,delete])
        return UIMenu(__title: "Menu", image: nil, identifier: nil, children:[save, edit])
    }
    return configuration
}
票数 7
EN

Stack Overflow用户

发布于 2019-08-06 20:39:28

在Xcode 11.0Beta 5中,您现在可以编写:

代码语言:javascript
复制
extension SingleViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu? in
            // Creating Save button
            let save = UIAction(title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill")) { action in
                // Just showing some alert
                self.showAlert(title: action.title)
            }

            // Creating Rotate button
            let rotate = UIAction(title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise")) { action in
                self.showAlert(title: action.title)
            }
            // Creating Delete button
            let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), attributes: .destructive) { action in
                self.showAlert(title: action.title)
            }
            // Creating Edit, which will open Submenu
            let edit = UIMenu(title: "Edit...", children: [rotate, delete])

            // Creating main context menu
            return UIMenu(title: "Menu", children: [save, edit])
        }
        return configuration
    }
}
  • 这些类不再是泛型的,例如UIMenu而不是UIMenu<UIAction>
  • 综合Swift初始化器允许您删除可选的参数,这些参数具有合理的默认值,例如imageattributesidentifier
  • 不再是UIMenu<UIAction>.create(...)UIAction(__title:...)。他们现在都是真正的初始化者了,耶!
票数 3
EN

Stack Overflow用户

发布于 2021-02-21 14:55:36

在斯威夫特5号,这一工作:

代码语言:javascript
复制
extension ViewController: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu? in
        // Creating Save button
        let save = UIAction(title: "Save", image: UIImage(systemName: "tray.and.arrow.down.fill")) { action in
            // Just showing some alert
            self.showAlert(title: action.title)
        }

        // Creating Rotate button
        let rotate = UIAction(title: "Rotate", image: UIImage(systemName: "arrow.counterclockwise")) { action in
            self.showAlert(title: action.title)
        }
        // Creating Delete button
        let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill")) { action in
            self.showAlert(title: action.title)
        }
        // Creating Edit, which will open Submenu
        let edit = UIMenu(title: "Edit...", children: [rotate, delete])

        // Creating main context menu
        return UIMenu(title: "Menu", children: [save, edit])
    }
    return configuration
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56960374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档