swiftuinavigationcontrollernavigationcontrollerside-menu

Title incorrectly in UINavigationController


I am trying all the ways to change the title after going from the button to the home screen. The main screen shows me, and the title does not want to change in any way.I want the title to be shown "Главная". I've reviewed all of YouTube, downloaded projects from github, and I can't find a solution

Main controller:

protocol HomeViewControllerDelegate: AnyObject {
func didTapButtonMenu()
}

class HomeViewController: UIViewController {
weak var delegate: HomeViewControllerDelegate?
var primer = UILabel()


override func viewDidLoad() {
    super.viewDidLoad()
    primer.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    primer.backgroundColor = .red
    primer.text = ""
    view.addSubview(primer)


    view.backgroundColor = .white
    title = "Главная"

    navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "list.dash")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(barButtonTapped)) //убираем у иконки синий цвет с помощью withRenderingMode(.alwaysOriginal)
}

@objc func barButtonTapped() {
    delegate?.didTapButtonMenu()
}

Container controller:

extension ContainerViewController: MenuViewControllerDelegate {
func didSelect(menuItem: MenuViewController.MenuOptions) {
    toggleMenu(compltion: nil)
    switch menuItem {
    case .home:
        self.resetToHome()
    case .calendarPay:
        break
    case .statistics:
        break
    case .addProperty:
        self.addPropertyFunc()
    case .settings:
        break
    }
}

func addPropertyFunc() {
    let vc = addPropertyVC
    homeVC.addChild(vc)
    homeVC.view.addSubview(vc.view)
    vc.view.frame = view.frame
    vc.didMove(toParent: homeVC)
    homeVC.title = vc.title
    
}

func resetToHome() {
    addPropertyVC.view.removeFromSuperview()
    addPropertyVC.didMove(toParent: nil)
    homeVC.title = "Главная"
    
}

//Button in "3" controller

    func buttonNextFunc() {
    buttonNext.frame = CGRect(x: tableView.center.x - 75, y: tableView.bounds.height + 20, width: 150, height: 50)
    buttonNext.setTitle("Сохранить", for: .normal)
    buttonNext.backgroundColor = AllColors.surfColor()
    buttonNext.layer.cornerRadius = 10
    buttonNext.addTarget(nil, action: #selector(buttonNextTap), for: .touchUpInside)
    scrollView.addSubview(buttonNext)
}

@objc func buttonNextTap() {
    view.removeFromSuperview()
    didMove(toParent: nil)
    HomeViewController().title = "Главная"
}

enter image description here


Solution

  • The problem lies within this source code.

    HomeViewController().title = "Главная"
    

    You are creating a new instance of HomeViewController and assigning a title to it. A new instance that you don't have a reference to.

    The best way to accomplish your requirements based on what you have is to implement the required protocols within your HomeViewController

    extension HomeViewController: HomeViewControllerDelegate {
        func didTapButtonMenu() {
            title = "Главная"
        }
    }
    

    Migrate your delegate to your "3" View Controller

    weak var delegate: HomeViewControllerDelegate?
    
    @objc func buttonNextTap() {
        view.removeFromSuperview()
        didMove(toParent: nil)
        delegate?.didTapButtonMenu()
    }
    

    In this scenario, calling buttonNextTap calls delegate method to change the HomeViewController didTapButtonMenu to change its title.