UIAction, UIMenu

What is the correct syntax for UIMenu and UIAction in iOS 13, using Xcode 11.0 beta 3?


The following code was working in Xcode 11.0 beta 2


let action = UIAction(title: "title", image: image, options: []) { action in
     // some action
}

let menu = UIMenu(__title: "", image: nil, identifier: nil, children: [action])



The UIAction seems to have changed to something like


let action = UIAction(__title: "title", image: image, identifier: nil, handler: { _ in
     // some action
}


but this crashes with the error


+[UIAction actionWithTitle:image:identifier:handler:]: unrecognized selector sent to class 0x7fffa1b30178

[General] +[UIAction actionWithTitle:image:identifier:handler:]: unrecognized selector sent to class 0x7fffa1b30178

Replies

All those weird, unfinished syntax declarations in iOS 13 beta are a mess. It's not even in sync with the actual WWDC session examples! This is frustrating.

I was able to make it compile using the following syntax:


let action = UIAction(__title: "title", image: nil, identifier: nil) { _ in
     // some action
}


But in runtime on my iPhone 7 Plus (iOS 13 beta 2), it generated this error:


+[UIAction actionWithTitle:image:identifier:handler:]: unrecognized selector sent to class 0x1cabaf5a0

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIAction actionWithTitle:image:identifier:handler:]: unrecognized selector sent to class 0x1cabaf5a0'


I think this problem only occurs on iOS 13 beta 2, because I was able to successfully execute this code on the iPad Pro simulator.

Same problem here, with beta 3 on iPhone 7 Plus, it crashes.

This is now fixed in Beta 5, the __title initializers are not necessary anymore. For example:


private func createContextMenu() -> UIContextMenuConfiguration {

    let actionProvider: UIContextMenuActionProvider = { _ in

        let showMap = UIAction(title: "Show map", image: UIImage(systemName: "mappin.and.ellipse")) { _ in
            // ...
        }
        return UIMenu(title: "Example menu", children: [showMap])
    }

    return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: actionProvider)
}