简体   繁体   English

如何更改UIAlertAction的标题

[英]How to change the title of an UIAlertAction

How can I change the title of an UIAlertAction when I click the button ? 单击按钮时如何更改UIAlertAction的标题? I want to click that button and from "Enable" to make it "Disable" for example. 我想单击该按钮,然后从“启用”使其变为“禁用”。 I spent a lot of time trying to achieve this but I can't manage to do it. 我花了很多时间尝试实现这一目标,但我无法做到。 Here is a small Demo with my issue: https://github.com/tygruletz/ChangeTitleOfAlertAction 这是一个有关我的问题的小演示: https : //github.com/tygruletz/ChangeTitleOfAlertAction

Here is my code: 这是我的代码:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        tableView.tableFooterView = UIView()

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "Row \(indexPath.row)"

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        showOptions()
    }

    func showOptions(){

        var enable = "Enable"
        let disable = "Disable"

        let applyOn = UIAlertAction(title: enable, style: .default, handler: { (action: UIAlertAction!) in

            enable = disable
        })

        let actionSheet = configureActionSheet()
        actionSheet.addAction(applyOn)

        self.present(actionSheet, animated: true, completion: nil)
    }

    func configureActionSheet() -> UIAlertController {
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        actionSheet.addAction(cancel)

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad ){
            actionSheet.popoverPresentationController?.sourceView = self.view
            actionSheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            actionSheet.popoverPresentationController?.permittedArrowDirections = []
        }

        return actionSheet
    }
}

And here is a capture of screen: 这是屏幕截图:

在此处输入图片说明

Thank you if you try to help me ! 谢谢您的帮助!

Please follow below code: 请遵循以下代码:

Define property in your UIViewController UIViewController定义属性

var selectedIndexPath:IndexPath!

Add argument in showOptions method showOptions方法中添加参数

   func showOptions(indexPath:IndexPath){

    var status = "Enable"

    if selectedIndexPath == indexPath{
        status = "Disable"
    }

    let applyOn = UIAlertAction(title: status, style: .default, handler: { (action: UIAlertAction!) in
        if self.selectedIndexPath == indexPath{
            self.selectedIndexPath = nil
        }else{
            self.selectedIndexPath = indexPath
        }
    })

    let actionSheet = configureActionSheet()
    actionSheet.addAction(applyOn)

    self.present(actionSheet, animated: true, completion: nil)
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    showOptions(indexPath: indexPath)
}

Note: 注意:

If you are going with this approach, Then you will never faced cell usability issue. 如果您采用这种方法,那么您将永远不会遇到单元可用性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM