Monday, December 11, 2023
HomeiOS Developmentios - The right way to save button states in Swift?

ios – The right way to save button states in Swift?


I’ve a controller with TableView, within the didSelectRowAt() methodology, whenever you click on on a cell, it goes to a different controller. This controller has a button that adjustments its picture when clicked. How do I make the state of the button persist relying on which cell the button was clicked in?

The code of the controller with the button:

class ArticleViewController: UIViewController {
 
    var isSave = true
 
    override func viewDidLoad() {
    
        self.updateRighBarButton(isSave: isSave)
    }
    
    personal func updateRighBarButton(isSave: Bool) {
        let btnSave = UIButton(body: CGRectMake(0,0,30,30))
        btnSave.addTarget(self, motion: #selector (btnSavesDidTap), for: .touchUpInside)
        // Importing picture to the button
        if isSave {
            btnSave.setImage(UIImage(systemName: "bookmark"), for: .regular)
        } else {
            btnSave.setImage(UIImage(systemName: "bookmark.fill"), for: .regular)
        }
        let rightButton = UIBarButtonItem(customView: btnSave)
        self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
    }
    // That is the place the button is pressed
    @objc personal func btnSavesDidTap() {
        self.isSave = !self.isSave
        if self.isSave {
            self.saved()
        } else {
            self.unsaved()
        }
        self.updateRighBarButton(isSave: self.isSave)
    }
    
    personal func saved() {
    }
    
    personal func unsaved() {
    }
}

Controller code with tableView:

// Mannequin
class Part {
    let title: String
    let choices: [String]?
    var isOpenned: Bool = false

    init(title: String, choices: [String]?, isOpenned: Bool = false) {
        self.title = title
        self.choices = choices
        self.isOpenned = isOpenned
    }
}

class MedecineViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    personal var sections = [Section]()
    var articlesArr = [NSAttributedString]()
    let articles = ArticlesForMedicineChapter()

    override func viewDidLoad() {
        tremendous.viewDidLoad()
       
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", model: .plain, goal: nil, motion: nil)
        tableView.backgroundView = UIImageView(picture: UIImage(named: "1"))
        tableView.separatorStyle = .none
        
        // Arrange fashions
        sections = []
        
        articles.passData(arr: &articlesArr)
    }
}

extension MedecineViewController: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        sections.rely
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        let part = sections[section]
        
        if part.isOpenned {
            return (part.choices?.rely ?? 0) + 1
        } else {
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MedecineTableViewCell
        cell?.uIView.layer.cornerRadius = (cell?.uIView.body.top)! / 2
        if indexPath.row == 0 {
            cell?.titleLabel.textColor = .white
            cell?.uIView.backgroundColor = .darkGray
            cell?.titleLabel.textual content = sections[indexPath.section].title
        } else {
            cell?.uIView.backgroundColor = .purple
            cell?.titleLabel.textColor = .white
            cell?.titleLabel.textual content = sections[indexPath.section].choices?[indexPath.row - 1]
        }
        
        return cell ?? UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let index = tableView.cellForRow(at: indexPath)
        if sections[indexPath.section].choices == nil {
            if sections[indexPath.section].title == "Textual content" {
                let medicineVc = storyboard?.instantiateViewController(withIdentifier: "ArticleViewController") as? ArticleViewController
                medicineVc?.textual content = articlesArr[0]
                medicineVc?.chapterText = "Textual content"
                navigationController?.pushViewController(medicineVc!, animated: true)
            } else if sections[indexPath.section].title == "Textual content" {
                let medicineVc = storyboard?.instantiateViewController(withIdentifier: "ArticleViewController") as? ArticleViewController
                medicineVc?.textual content = articlesArr[1]
                medicineVc?.chapterText = "Textual content"
                navigationController?.pushViewController(medicineVc!, animated: true)
            }
        }
        
        if indexPath.row == 0 {
            sections[indexPath.section].isOpenned = !sections[indexPath.section].isOpenned
            tableView.reloadSections([indexPath.section], with: .none)
        } else  if index == tableView.cellForRow(at: IndexPath(row: 1, part: 1))  {
            let medicineVc = storyboard?.instantiateViewController(withIdentifier: "ArticleViewController") as? ArticleViewController
            medicineVc?.textual content = articlesArr[2]
            medicineVc?.chapterText = "Textual content"
            navigationController?.pushViewController(medicineVc!, animated: true)
        } else  if index == tableView.cellForRow(at: IndexPath(row: 2, part: 1)) {
            let medicineVc = storyboard?.instantiateViewController(withIdentifier: "ArticleViewController") as? ArticleViewController
            medicineVc?.textual content = articlesArr[3]
            medicineVc?.chapterText = "Textual content"
            navigationController?.pushViewController(medicineVc!, animated: true)
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments