我正在尝试使用iOS 13.0Beta中引入的新API。我下载了Xcode 11.0Beta 3来访问这些API。
我在网上找到的一些代码做了如下工作:
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
关于创建保存操作。
还有其他几个类似的错误。
我还应该指出,我甚至没有这种格式的构造函数:
UIAction(__title: "String", image: UIImage, options: Array)
UIMenu.create(...)为什么在Xcode-11 beta 3.0中缺少这些?
发布于 2019-07-09 21:42:04
嗯,它变了。这是个测试版!我希望它会再次改变,但是现在,在beta 3中,UIAction的初始化程序是
init(__title title: String, image: UIImage?, identifier: UIAction.Identifier?,
handler: @escaping UIActionHandler)UIMenu不是泛型的,它的初始化程序是
init(__title title: String, image: UIImage?, identifier: UIMenu.Identifier?,
options: UIMenu.Options = [], children: [UIMenuElement])下面是对在beta 3下编译的代码的重写:
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
}发布于 2019-08-06 20:39:28
在Xcode 11.0Beta 5中,您现在可以编写:
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>。image、attributes和identifier。UIMenu<UIAction>.create(...)或UIAction(__title:...)。他们现在都是真正的初始化者了,耶!发布于 2021-02-21 14:55:36
在斯威夫特5号,这一工作:
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
}
}https://stackoverflow.com/questions/56960374
复制相似问题