swiftuibuttonuialertcontrolleruiactionsheetiboutlet

Set title label of two action sheet buttons


when i choose an option from action sheet, it set a title label to button. But when i click another button with another action sheet, it cancel title label of first button. How can i set each button without cancel any title label?

This is how it works:

enter image description here

This is my code of two action sheets:


@IBAction func paymentMethodActionSheet(_ sender: Any) {
        PaymentMethodTitle.titleLabel?.text = "Seleziona"
    
    let optionMenu = UIAlertController(title: nil, message: "Scegli il metodo di pagamento", preferredStyle: .actionSheet)
        let cardAction = UIAlertAction(title: "Paga con carta", style: .default, handler: { action in
            self.PaymentMethodTitle.titleLabel?.text = "Paga con carta"
            self.PaymentMethodTitle.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right

        })
        let contantiAction = UIAlertAction(title: "Contanti", style: .default, handler: { action in
            self.PaymentMethodTitle.titleLabel?.text = "Contanti"
            self.PaymentMethodTitle.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right

            
        })
    
    let cancelAction = UIAlertAction(title: "Cancella", style: .cancel)
    
        optionMenu.addAction(cardAction)
        optionMenu.addAction(contantiAction)
        optionMenu.addAction(cancelAction)
        
        if let popoverController = optionMenu.popoverPresentationController {
            popoverController.barButtonItem = sender as? UIBarButtonItem
            popoverController.sourceView = self.view
            popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }
        
        self.present(optionMenu, animated: true, completion: nil)
    
    }
    
    @IBAction func shippingMethodActionSheet(_ sender: Any) {
        
        shippingMethodTitle.titleLabel?.text = "Seleziona"
        
        let option2Menu = UIAlertController(title: nil, message: "Scegli l'opzione d'acquisto", preferredStyle: .actionSheet)
            let houseAction = UIAlertAction(title: "Consegna a domicilio", style: .default, handler: { action in
                self.shippingMethodTitle.titleLabel?.text = "Consegna a domicilio"
                self.shippingMethodTitle.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right

            })
            let businessAction = UIAlertAction(title: "Ritiro presso attività", style: .default, handler: { action in
                self.shippingMethodTitle.titleLabel?.text = "Ritiro presso attività"
                self.shippingMethodTitle.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right

                
            })
        
        let cancel2Action = UIAlertAction(title: "Cancella", style: .cancel)
        
            option2Menu.addAction(houseAction)
            option2Menu.addAction(businessAction)
            option2Menu.addAction(cancel2Action)
            
            if let popoverController2 = option2Menu.popoverPresentationController {
                popoverController2.barButtonItem = sender as? UIBarButtonItem
                popoverController2.sourceView = self.view
                popoverController2.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
                popoverController2.permittedArrowDirections = []
            }
            
            self.present(option2Menu, animated: true, completion: nil)
        
    }


Solution

  • Instead of using self.PaymenMethodTitle?.titleLabel.text = "Contanti"to change the button title inside closure, just Use self.PaymentMethodTitle.setTitle("Contanti", for: .normal). I've tried it myself and Its working Fine.

    Code:

    class ViewController: UIViewController {
    
    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    
    @IBAction func btn1Pressed(_ sender: Any) {
        
        let optionMenu = UIAlertController(title: nil, message: "Scegli il metodo di pagamento", preferredStyle: .actionSheet)
            let cardAction = UIAlertAction(title: "Paga con carta", style: .default, handler: { action in
                self.btn1.setTitle("Paga con carta", for: .normal)
                self.btn1.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
    
            })
            let contantiAction = UIAlertAction(title: "Contanti", style: .default, handler: { action in
                self.btn1.setTitle("Conati", for: .normal)
                self.btn1.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
    
                
            })
        
        let cancelAction = UIAlertAction(title: "Cancella", style: .cancel)
        
            optionMenu.addAction(cardAction)
            optionMenu.addAction(contantiAction)
            optionMenu.addAction(cancelAction)
            
            if let popoverController = optionMenu.popoverPresentationController {
                popoverController.barButtonItem = sender as? UIBarButtonItem
                popoverController.sourceView = self.view
                popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
                popoverController.permittedArrowDirections = []
            }
            
            self.present(optionMenu, animated: true, completion: nil)
    }
    
    
    @IBAction func btn2Pressed(_ sender: Any) {
        
        let option2Menu = UIAlertController(title: nil, message: "Scegli l'opzione d'acquisto", preferredStyle: .actionSheet)
            let houseAction = UIAlertAction(title: "Consegna a domicilio", style: .default, handler: { action in
                self.btn2.setTitle("Consegna a domicilio", for: .normal)
                self.btn2.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
    
            })
            let businessAction = UIAlertAction(title: "Ritiro presso attività", style: .default, handler: { action in
                self.btn2.setTitle("Ritiro presso attività", for: .normal)
                self.btn2.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
    
                
            })
        
        let cancel2Action = UIAlertAction(title: "Cancella", style: .cancel)
        
            option2Menu.addAction(houseAction)
            option2Menu.addAction(businessAction)
            option2Menu.addAction(cancel2Action)
            
            if let popoverController2 = option2Menu.popoverPresentationController {
                popoverController2.barButtonItem = sender as? UIBarButtonItem
                popoverController2.sourceView = self.view
                popoverController2.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
                popoverController2.permittedArrowDirections = []
            }
            
            self.present(option2Menu, animated: true, completion: nil)
    }
    
    
     }
    

    Simulator ScreenShot:

    Simulator Picture