UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Ways to let the user pick from a set number of choices (Enum)

Forums > iOS

I have this enum, which defines the type of annotations in my app. It is also used for the sections of my diffable data source. When adding a new annotation the user has to pick a type for that annotation. I have this viewcontroller (form) where the user inputs all data. My first thought would be a dropdown list for the types, but there's no such thing in swift. I have some ideas how to tackle this, but I would like to hear ideas from those who might have done this before ...

3      

I think that can be done with either a UIPickerView or a UISegmentedControl.

4      

Thank you, Segmented control doesn't really suit my needs, but i'll look into the picker view. UIMenu isn't available on iOS13, right ?

I'll have a look at UIMenu as well

3      

I solved it using a context menu:

In my view controller:

        let interaction = UIContextMenuInteraction(delegate: self)
        annotationTypeButton.addInteraction(interaction)

And the delegate as an extension:

//MARK: - UIContextMenuInteractionDelegate
extension AnnotationDetailsViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        return UIContextMenuConfiguration(identifier: "annotationTypeMenu" as NSCopying, previewProvider: nil) { _ in
            let children: [UIMenuElement] = self.makeAnnotationTypeActions()
            return UIMenu(title: "", children: children)
        }
    }

    func makeAnnotationTypeActions() -> [UIAction] {
        var actions = [UIAction]()
        for type in AnnotationType.allCases {
            actions.append( UIAction(title: type.rawValue, image: type.image, identifier: nil, attributes: []) { _ in
                let annotationType = AnnotationType(rawValue: type.rawValue) ?? AnnotationType.tips
                self.annotation.type = annotationType
                self.configureAnnotationTypeButton(with: annotationType)
            })
        }
        return actions
    }
}

Works nicely, but I do get an autolayout error in the console on opening the menu ...

4      

If anyone can help with my autolayout warning: https://stackoverflow.com/questions/62661755/swift-programmatic-ui-uicontextmenuinteraction-auto-layout-error-groupview-on

I see more people are having these layout warnings, see the remark at the end of this post https://useyourloaf.com/blog/adding-context-menus-in-ios-13/

3      

I got an answer on the Apple developer forums saying that the layout warning is a known issue and will be solved in iOS14!

https://developer.apple.com/forums/thread/652622?login=true&page=1#618265022

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.