xcode 如何快速设置动作 backBarButtonItem?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26421938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set action backBarButtonItem in swift?
提问by Aditya Dharma
How to to set action backBarButtonItem and still display left arrow?
如何设置动作 backBarButtonItem 并仍然显示左箭头?
I used this code but the arrow was not display
我使用了这段代码,但没有显示箭头
var barBack = UIBarButtonItem(title: "Reset", style: UIBarButtonItemStyle.Plain, target: self, action: "reset:")
self.navigationItem.leftBarButtonItem = barBack
but when I used this code the action not working
但是当我使用此代码时,该操作不起作用
self.navigationController?.navigationBar.topItem?.backBarButtonItem = barBack
Thanks
谢谢
回答by David James
As an alternative to setting an action, you can keep the backBarButtonItem
and use isMovingFromParentViewController
instead to handle logic for moving back on the navigation stack.
作为设置操作的替代方法,您可以保留backBarButtonItem
和使用isMovingFromParentViewController
来处理返回导航堆栈的逻辑。
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if isMovingFromParentViewController() {
.. do something here
}
}
回答by Dragos Strugar
Try this:
In ViewDidLoad
试试这个:在 ViewDidLoad
let backButton = UIBarButtonItem(title: "< Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "the_font_you_want_to_use", size: size_of_the_font_this_should_be_integer)!], forState: UIControlState.Normal)
and implement the following method:
并实现以下方法:
func goBack() {
self.navigationController?.popToRootViewControllerAnimated(boolean)
}
Just replace the the_font_you_want_to_use, size_of_the_font_this_should_be_integer and boolean with the values you want, and everything will be working.
只需将 the_font_you_want_to_use、size_of_the_font_this_should_be_integer 和 boolean 替换为您想要的值,一切都会正常进行。
*** EDIT
*** 编辑
If you don't want to get back to Root View Controller
, instead of
如果你不想回到Root View Controller
,而不是
self.navigationController?.popToRootViewControllerAnimated(boolean)
you can say:
你可以说:
self.navigationController?.popViewControllerAnimated(boolean)
or even pop to some other ViewController
with specifying the first parameter of the popToViewController
to be the object of YourViewController
class, and second the boolean value for animation.
或者甚至ViewController
通过将 的第一个参数指定popToViewController
为YourViewController
类的对象,然后指定动画的布尔值来弹出到其他一些。
回答by Rajeev Bhatia
Try this
尝试这个
var barBack = UIBarButtonItem(title: "Reset", style: UIBarButtonItemStyle.Plain, target: self, action: "reset:")
self.navigationItem.leftBarButtonItem = barBack
Let me know if it works.
让我知道它是否有效。