UIMenu doen't change its value if the user selects an element for the second time

42 Views Asked by At

I have the code below that shows a list for the user with 5 options. After the user selects an option for the first time, it show a list with 4 options.

But if the user selecta a new option for the second or third time, the values don't change.

fileprivate func configMenu() {
    
    let optionClosure = { [weak self]
        ( action : UIAction ) in
        print("v000: \(action.title)")
        if action.title != "Selecione a chave" {
            
            self?.accountTypePopUpButton.menu = UIMenu(children: [
                UIAction(title: "CPF/CNPJ", handler: { _ in } ),
                UIAction(title: "Celular", handler: { _ in } ),
                UIAction(title: "E-mail", handler: { _ in } ),
                UIAction(title: "Chave aleatória", handler: { _ in } ),                    
            ])
            self?.accountTypeFilled = true
            self?.accountType = action.title
            self?.accountTypePopUpButton.reloadInputViews()
        }

    }
    
    accountTypePopUpButton.menu = UIMenu(children: [
        UIAction(title: "Selecione a chave", handler: optionClosure),
        UIAction(title: "CPF/CNPJ", handler: optionClosure),
        UIAction(title: "Celular", handler: optionClosure),
        UIAction(title: "E-mail", handler: optionClosure),
        UIAction(title: "Chave aleatória", handler: optionClosure),
    ])
    
    accountTypePopUpButton.showsMenuAsPrimaryAction = true
    accountTypePopUpButton.changesSelectionAsPrimaryAction = true
    
}

How can I fix this?

2

There are 2 best solutions below

0
fabiobh On BEST ANSWER

This is what fix the code for me.

    let optionClosure = { [weak self] ( action : UIAction ) in
        print("v000: \(action.title)")
        if action.title != "Selecione a chave pix" {
            self?.accountTypePopUpButton.setTitleColor(.savvyBlack, for: .normal)
            self?.accountTypePopUpButton.menu = UIMenu(children: [
                UIAction(title: "CPF/CNPJ", handler: { [weak self] ( myAction : UIAction ) in self?.setActionValues(myAction.title) } ),
                UIAction(title: "Celular", handler: { [weak self] ( myAction : UIAction ) in self?.setActionValues(myAction.title) } ),
                UIAction(title: "E-mail", handler: { [weak self] ( myAction : UIAction ) in self?.setActionValues(myAction.title) } ),
                UIAction(title: "Chave aleatória", handler: { [weak self] ( myAction : UIAction ) in self?.setActionValues(myAction.title) } ),
            ])
            self?.setActionValues(action.title)
        }

    }

fileprivate func setActionValues(_ actionTitle: String) {
    dprint("actionTitle v1: \(actionTitle)")
    accountTypeFilled = true
    accountType = actionTitle
    accountTypePopUpButton.reloadInputViews()
}

To fix my code what I need is to modify the empty handler code

handler: { _ in } 

to

handler: { [weak self] ( myAction : UIAction ) in self?.setActionValues(myAction.title) } 

When I modify this handler, the code starts working for me.

To fix my code I just need to put a code inside the handler. The 2nd handler is where I need to run my code after it change the Menu options.

3
Navneet Kaur On
fileprivate func configMenu() {

var selectionCount = 0

let optionClosure = { [weak self]
    ( action : UIAction ) in
    print("v000: \(action.title)")
    
    if action.title != "Selecione a chave" {
        // Update menu only if the user hasn't selected an option before
        if selectionCount == 0 {
            self?.accountTypePopUpButton.menu = UIMenu(children: [
                UIAction(title: "CPF/CNPJ", handler: { _ in } ),
                UIAction(title: "Celular", handler: { _ in } ),
                UIAction(title: "E-mail", handler: { _ in } ),
                UIAction(title: "Chave aleatória", handler: { _ in } ),
            ])
            self?.accountTypePopUpButton.reloadInputViews()
        }
        
        self?.accountTypeFilled = true
        self?.accountType = action.title
        selectionCount += 1
    }
}

accountTypePopUpButton.menu = UIMenu(children: [
    UIAction(title: "Selecione a chave", handler: optionClosure),
    UIAction(title: "CPF/CNPJ", handler: optionClosure),
    UIAction(title: "Celular", handler: optionClosure),
    UIAction(title: "E-mail", handler: optionClosure),
    UIAction(title: "Chave aleatória", handler: optionClosure),
])

accountTypePopUpButton.showsMenuAsPrimaryAction = true
accountTypePopUpButton.changesSelectionAsPrimaryAction = true

}

This is how you can modify your code and get it fixed.