Swift 设置两个操作表按钮的标题标签

Swift 设置两个操作表按钮的标题标签,swift,uibutton,uialertcontroller,uiactionsheet,iboutlet,Swift,Uibutton,Uialertcontroller,Uiactionsheet,Iboutlet,当我从“操作表”中选择一个选项时,它会将标题标签设置为“按钮”。但当我用另一个操作表单击另一个按钮时,它会取消第一个按钮的标题标签。如何设置每个按钮而不取消任何标题标签 这就是它的工作原理: 这是我的两张行动表代码: @IBAction func paymentMethodActionSheet(_ sender: Any) { PaymentMethodTitle.titleLabel?.text = "Seleziona" let o

当我从“操作表”中选择一个选项时,它会将标题标签设置为“按钮”。但当我用另一个操作表单击另一个按钮时,它会取消第一个按钮的标题标签。如何设置每个按钮而不取消任何标题标签

这就是它的工作原理:

这是我的两张行动表代码:


@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.PaymentMethodTitle?.titleLabel.text=“Contanti”来更改闭包内的按钮标题,只需使用self.PaymentMethodTitle.setTitle(“Contanti”,用于:。正常)。 我自己也试过,效果很好

代码:

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


 }
模拟器屏幕截图:

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


 }

不要使用
self.PaymenMethodTitle?.titleLabel.text=“Contanti”
来更改闭包内的按钮标题,只需使用
self.PaymentMethodTitle.setTitle(“Contanti”,用于:。正常)
。 我自己也试过,效果很好

代码:

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


 }
模拟器屏幕截图:

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


 }

我试过了,文本也没有改变。我在同一个视图中搜索了关于使用更多Popover的信息,似乎不可能感谢您。我不得不改变标题归于普莱恩,我试过了,文本也没有改变。我在同一个视图中搜索了关于使用更多Popover的信息,似乎不可能感谢您。我不得不把头衔改成普通人