UINavigationController 中的标题不正确
Title incorrectly in UINavigationController
从按钮转到主屏幕后,我正在尝试所有更改标题的方法。主屏幕显示我,标题不想更改 way.I 希望标题显示为“Главная”。我查看了所有 YouTube,从 github 下载了项目,但找不到解决方案
主控制器:
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()
}
容器控制器:
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 = "Главная"
}
//“3”控制器中的按钮
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 = "Главная"
}
问题出在这个源代码中。
HomeViewController().title = "Главная"
您正在创建 HomeViewController 的新实例并为其分配标题。您没有引用的新实例。
根据您所拥有的满足您的要求的最佳方法是在您的 HomeViewController
中实施所需的协议
extension HomeViewController: HomeViewControllerDelegate {
func didTapButtonMenu() {
title = "Главная"
}
}
将您的委托迁移到您的“3
”视图控制器
weak var delegate: HomeViewControllerDelegate?
@objc func buttonNextTap() {
view.removeFromSuperview()
didMove(toParent: nil)
delegate?.didTapButtonMenu()
}
在这种情况下,调用 buttonNextTap
调用委托方法来更改 HomeViewController
didTapButtonMenu
以更改其标题。
从按钮转到主屏幕后,我正在尝试所有更改标题的方法。主屏幕显示我,标题不想更改 way.I 希望标题显示为“Главная”。我查看了所有 YouTube,从 github 下载了项目,但找不到解决方案
主控制器:
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()
}
容器控制器:
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 = "Главная"
}
//“3”控制器中的按钮
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 = "Главная"
}
问题出在这个源代码中。
HomeViewController().title = "Главная"
您正在创建 HomeViewController 的新实例并为其分配标题。您没有引用的新实例。
根据您所拥有的满足您的要求的最佳方法是在您的 HomeViewController
extension HomeViewController: HomeViewControllerDelegate {
func didTapButtonMenu() {
title = "Главная"
}
}
将您的委托迁移到您的“3
”视图控制器
weak var delegate: HomeViewControllerDelegate?
@objc func buttonNextTap() {
view.removeFromSuperview()
didMove(toParent: nil)
delegate?.didTapButtonMenu()
}
在这种情况下,调用 buttonNextTap
调用委托方法来更改 HomeViewController
didTapButtonMenu
以更改其标题。