1

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

PollyVern
  • 39
  • 8

1 Answers1

1

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.

Mochi
  • 1,059
  • 11
  • 26
  • I did everything as you wrote. But the problem was not solved. I output it to the console print(delegate?.didTapButtonMenu()) and nil returns – PollyVern Jun 13 '21 at 06:20
  • Did you set the delegate correctly when you created your "3" ViewController? – Mochi Jun 13 '21 at 06:38
  • class viewController3: UIViewController { weak var delegate: HomeViewControllerDelegate? var buttonNext = UIButton()} – PollyVern Jun 13 '21 at 06:51
  • How do you navigate from HomeViewController to viewController3? When do you instantiate viewController3? You need to set the delegate to self to HomeViewController. It should look like this viewController3.delegate = self – Mochi Jun 13 '21 at 14:08
  • no. It turns out that I need to create another protocol? – PollyVern Jun 13 '21 at 14:47
  • HomeViewControllerDelegate is the protocol you need. You just need to assign to self (HomeViewController) as you navigate from HomeViewController to viewController3 – Mochi Jun 13 '21 at 15:46
  • thanks. I added something else and it worked – PollyVern Jun 14 '21 at 16:22