首页 > 解决方案 > 设置两个操作表按钮的标题标签

问题描述

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

这是它的工作原理:

在此处输入图像描述

这是我的两个操作表的代码:


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

标签: swiftuibuttonuialertcontrolleruiactionsheetiboutlet

解决方案


而不是使用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)
}


 }

模拟器截图:

模拟器图片


推荐阅读