Xcode pop up menu, how to create action based of selection (EASY)

40 Views Asked by At

I am trying to make the pop up menu such that as long as the selection is not the default "University", then UniversityLabel.textColor = UIColor.systemGreen, otherwise if the section is still on default then the UniversityLabel.textColor = UIColor.systemRed

That's it!

Pop Up Menu Code

func UniPop(){
        let Uni = {
            (action: UIAction) in
            print(action.title)
        }
        University.menu = UIMenu(children: [
            UIAction(title: "University of Warwick", handler: Uni),
            UIAction(title: "University", state: .on, handler: Uni)
        ])
        University.showsMenuAsPrimaryAction = true
        University.changesSelectionAsPrimaryAction = true
        
        
        if let uni = University.menu?.selectedElements.first?.title

        {
            if let errorMessage = InvalidUni(uni)
            {
                UniversityLabel.textColor = UIColor.systemRed
                UniversityLabel.text = errorMessage
            }
            else {
                UniversityLabel.textColor = UIColor.systemGreen
            }
        }
        checkForValidForm()
    }
    

        func InvalidUni(_ value: String) -> String?
    {
        let reqularExpression = "University"
        let predicate = NSPredicate(format: "SELF MATCHES %@", reqularExpression)
        if !predicate.evaluate(with: value)
        {
            return "."
        }
        return nil
        }

I tried creating a regex function:

        func InvalidUni(_ value: String) -> String?
    {
        let reqularExpression = "University"
        let predicate = NSPredicate(format: "SELF MATCHES %@", reqularExpression)
        if !predicate.evaluate(with: value)
        {
            return "."
        }
        return nil
        }

if the University.menu?.selectedElements.first?.title = "University",then UniversityLabel.textColor = UIColor.systemRed.

Currently, when I run the code UniversityLabel.textColor = UIColor.systemGreen, even when another option is selected from the pop menu.

0

There are 0 best solutions below