在ViewController上设置默认的后退按钮

时间:2018-08-31 13:05:05

标签: ios swift4 back-button uinavigationitem

short :我不了解如何以编程方式添加默认后退按钮。

long :系统要求我编写一个iOS应用,但我没有任何以前的经验 遵循Nicola Zaghini给出的建议和代码。

在与文章一起提供的code中,我真的不明白 后退按钮从哪里来。

该应用具有三个屏幕

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

+有一个WeatherLocation按钮:

enter image description here

此按钮已添加到代码中,但找不到 以及WeatherDetails中后退按钮的编码方式(请参见上文),以及单击后退按钮时执行的操作的编码方式。

enter image description here

我在网上搜索,发现如何在NavigationBar中设置按钮:

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和情节提要中都没有。

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

1 个答案:

答案 0 :(得分: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)
    }
}