简短:我不明白如何以编程方式添加默认后退按钮。
很久:我被要求编写一个iOS应用程序,没有任何以前的经验我决定遵循Nicola Zaghini给出的建议和代码。
在与文章一起给出的code中,我真的不明白后退按钮来自哪里。
该应用程序有三个屏幕
+
有WeatherLocation
按钮:
这个按钮被添加到代码中,但我找不到WeatherDetails
中的后退按钮编码的位置和方式(见上文),以及如何编码单击后退按钮时要执行的操作。
我在网上搜索并找到了如何在导航栏中设置按钮:
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中启用后退按钮。
有人可以给我一些关于如何设置后退按钮的提示吗?
非常感谢!!
在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)
}
}