在ViewController上设置默认后退按钮

问题描述 投票:0回答:1

简短:我不明白如何以编程方式添加默认后退按钮。

很久:我被要求编写一个iOS应用程序,没有任何以前的经验我决定遵循Nicola Zaghini给出的建议和代码。

在与文章一起给出的code中,我真的不明白后退按钮来自哪里。

该应用程序有三个屏幕

  • 一个选择一个城市(文件夹WeatherLocation)
  • 一个显示已经选择的所有城市(文件夹WeatherList)
  • 一个显示列表中单击的城市的天气(文件夹WeatherDetail)

+WeatherLocation按钮:

enter image description here

这个按钮被添加到代码中,但我找不到WeatherDetails中的后退按钮编码的位置和方式(见上文),以及如何编码单击后退按钮时要执行的操作。

enter image description here

我在网上搜索并找到了如何在导航栏中设置按钮:

let leftBarButtonItem: UIBarButtonItem = {
    let barButtonItem = UIBarButtonItem(title: "Left Item", style: .plain, target: self, action: nil)
    barButtonItem.tintColor = UIColor.red
    return barButtonItem
}()


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationItem.leftBarButtonItem = leftBarButtonItem
}

我还发现有一个backBarButtonItem但我找不到如何正确使用这个属性。

此外,在Nicola Zaghini的代码中,没有像backBarButtonItem那样在xib和storyboard中启用后退按钮。

有人可以给我一些关于如何设置后退按钮的提示吗?

ios swift4 back-button uinavigationitem
1个回答
0
投票

非常感谢!!

在Nicola Zaghini的code中,有一个默认后退按钮的秘密存在于(例如)类navigateToWeatherDetail中的WeatherListDefaultRouter类中,其中新的ViewController被推送到NavigationController

func navigateToWeatherDetail(withLocation location: Location) {
    if let weatherDetailVC = self.weatherDetailBuilder()?.buildWeatherDetailModule(withLocation: location) {
        self.viewController?.navigationController?.pushViewController(weatherDetailVC, animated: true)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.