将导航 backBarButtonItem 设置为转到 Swift 中的特定 UIViewController
set navigation backBarButtonItem to go to specific UIViewController in Swift
我需要导航的后退按钮始终弹出特定的 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 没有被调用。请帮忙。
谢谢。
这是给你的例子。
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作为例子供大家参考。
根据您的要求修改代码 need.may 这会对您有所帮助。
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
我需要导航的后退按钮始终弹出特定的 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 没有被调用。请帮忙。
谢谢。
这是给你的例子。
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作为例子供大家参考。
根据您的要求修改代码 need.may 这会对您有所帮助。
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