设置导航 backBarButtonItem 以转到 Swift 中的特定 UIViewController
Posted
技术标签:
【中文标题】设置导航 backBarButtonItem 以转到 Swift 中的特定 UIViewController【英文标题】:set navigation backBarButtonItem to go to specific UIViewController in Swift 【发布时间】:2015-02-27 10:37:50 【问题描述】:我需要导航的后退按钮总是弹出一个特定的 UIViewController。
override func viewDidLoad()
super.viewDidLoad()
//set title image
var logoImage:UIImage = UIImage(named: "barra")!
var logoImageView : UIImageView = UIImageView(image: logoImage)
logoImageView.frame = CGRectMake(0, 0, 320, 44)
logoImageView.contentMode = .ScaleAspectFit
logoImageView.contentMode = UIViewContentMode.Center
logoImageView.clipsToBounds = true
self.navigationItem.titleView = logoImageView
//set back image
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style: .Plain,target: self, action: "goToServizi:")
self.navigationController!.navigationBar.backIndicatorImage = backImage
self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = backImage
//set menu image
var menuImage:UIImage = UIImage(named: "menu")!
menuImage = menuImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationItem.rightBarButtonItem?.image = menuImage
func goToServizi(sender: UIBarButtonItem)
self.navigationController?.popToViewController(ServiziVC(), animated: true)// here I add a breakpoint, but it is never executed.
ServiziVC
是我每次点击返回按钮时需要显示的 UIViewController。
我不明白为什么不调用goToServizi
func。请帮忙。
谢谢。
【问题讨论】:
【参考方案1】:这是给你的例子。
import UIKit
class SecondViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
//Customise your barButton Like this
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var backButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.Bordered, target: self, action: "goToThird")
self.navigationItem.leftBarButtonItem = backButton
func goToThird()
//Initiate newViewController this way
let ThirdView = self.storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as ThirdViewController
self.navigationController?.pushViewController(ThirdView, animated: true)
编辑
单击要使用标识符启动的 ViewController,您可以在 Identity Inspector 中找到 StoryBoard ID,如下图所示。
HERE 是供您参考的示例。
根据您的需要修改代码。也许这会对您有所帮助。
【讨论】:
我必须在哪里为视图控制器添加标识符?这样我如何为后退按钮添加图像? 我试用了你的代码,它成功了。但我还有一个问题,我有大约 20 个带有自定义后退按钮的视图控制器,所以我必须将代码更改为你的。如何推送正确的视图? 你必须告诉每个按钮它会启动什么。【参考方案2】:self.navigationController?.popToViewController(ServiziVC(), animated: true) 你不能像那样自定义后退按钮动作。这有效:
override func viewDidLoad()
super.viewDidLoad()
self.navigationItem.hidesBackButton = true;
var backImage:UIImage = UIImage(named: "freccia")!
backImage = backImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.Bordered, target: self, action: "goToServizi:")
self.navigationItem.leftBarButtonItem = fakeBackButton;
func goToServizi(sender: UIBarButtonItem)
self.navigationController?.popToViewController(ServiziVC(), animated: true)
或者,您可以覆盖UINavigationController
的委托方法,如下所述:UINavigationController and back button action
【讨论】:
以上是关于设置导航 backBarButtonItem 以转到 Swift 中的特定 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章
将图像和标题都添加到导航 backBarButtonItem
如何使用外观代理设置 UINavigationItem 的 backBarButtonItem?
如何为 backBarButtonItem 设置自定义字体 [重复]