Tuesday, October 17, 2023
HomeiOS Developmentios - The best way to change colours primarily based on consumer...

ios – The best way to change colours primarily based on consumer choice utilizing NotificationCenter in UIKit?


I’m making an attempt to vary the colour of the textual content in a UITableView in UIKit. The thought is the consumer will go to a UIViewController known as AccentColorVC, the place they will select a coloration. I would like this to vary the colour of the textual content within the UITableView within the SettingsVC. I refactored the code a bit in order that just one cell will present up.

I’m making an attempt to make use of the NotificationCenter to implement this. Nevertheless, each time the consumer selects a coloration in AccentColorVC and tries return, the display screen finally ends up freezing, and I’m unable to return to SettingsVC. I am undecided why that is taking place.

Any assist can be enormously appreciated.

AccentColorVC – The place the consumer selects a coloration:

class AccentColorVC: UIViewController {
    
    let tableView = UITableView()
    let identifier = "AccentColorCell"
    
    let cells: [AccentColorTVCell] = [
        AccentColorTVCell(color: .systemRed, label: "Red", isChosen: false),
        AccentColorTVCell(color: .systemGreen, label: "Green", isChosen: false),
        AccentColorTVCell(color: .systemBlue, label: "Blue", isChosen: false),
    ]

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        configureViewController()
        configureTableView()
    }
    
    personal func configureViewController() {
        title = "Accent Shade"
        view.backgroundColor = .systemBackground
    }
    
    personal func configureTableView() {
        view.addSubview(tableView)
        
        tableView.rowHeight = 80
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    }
}

extension AccentColorVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return cells.rely
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return cells[indexPath.row]
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let selectedColor = cells[indexPath.row].coloration
        NotificationCenter.default.submit(title: .colorDidChange, object: nil, userInfo: ["color": selectedColor])
    }
    
}

SettingsVC – The place the consumer sees the colour being modified on the textual content.

import UIKit

class SettingsVC: UIViewController {
    
    let tableView = UITableView(body: .zero, fashion: .insetGrouped)
    let toggleSwitch = UISwitch()
    var selectedColor: UIColor = .label
    let identifier = "SettingsCell"
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        configureViewController()
        configureTableView()
        
        NotificationCenter.default.addObserver(self, selector: #selector(didReceiveColorChangeNotification(_:)), title: .colorDidChange, object: nil)
        
    }
    
    @objc func didReceiveColorChangeNotification(_ notification: Notification) {
        guard let coloration = notification.userInfo?["color"] as? UIColor else { return }
        self.selectedColor = coloration
        self.tableView.reloadData()
    }
    
    personal func configureViewController() {
        title = "Settings"
        view.backgroundColor = .systemBackground
    }
    
    personal func configureTableView() {
        view.addSubview(tableView)
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: identifier)
    }
}

extension SettingsVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        
        if indexPath.row == 0 {
            cell.textLabel?.textual content = "Change Accent Shade"
            cell.textLabel?.textColor = selectedColor // THIS IS WHERE I WANT THE CHANGE BEING MADE
            cell.accessoryType = .disclosureIndicator
            
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            print("Change Accent Shade chosen")
            navigationController?.pushViewController(AccentColorVC(), animated: true)
        }
        
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments