设置两个动作 sheet 按钮的标题标签

Set title label of two action sheet buttons

当我从操作 sheet 中选择一个选项时,它会将标题标签设置为按钮。但是当我用另一个动作点击另一个按钮时 sheet,它取消了第一个按钮的标题标签。如何在不取消任何标题标签的情况下设置每个按钮?

这是它的工作原理:

这是我的两个动作的代码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)
        
    }

不要使用 self.PaymenMethodTitle?.titleLabel.text = "Contanti" 更改闭包内的按钮标题,只需使用 self.PaymentMethodTitle.setTitle("Contanti", for: .normal)。 我自己试过了,效果很好。

代码:

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)
}


 }

模拟器截图: