NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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 ...

   

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

1      

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

   

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 ...

1      

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/

   

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

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.