ios 如何为 UIBarButtonItem 设置标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28210818/
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 do I set title for UIBarButtonItem?
提问by KSL
I'm a beginner iPhone developer.
我是初学者 iPhone 开发人员。
How can I programmatically set the title for the UIBarButtonItem
?
如何以编程方式设置UIBarButtonItem
?
My code is the following:
我的代码如下:
self.navigationItem.rightBarButtonItems =
UIBarButtonItem(
barButtonSystemItem: .Cancel, target: self,
action: "barButtonItemClicked:")
@IBAction func barButtonItemClicked(sender: UIBarButtonItem) {
//print something
}
回答by Kirsteins
Use different initialiser that allows you to specify the title:
使用不同的初始化程序,允许您指定标题:
UIBarButtonItem(title: "title", style: .Plain, target: self, action: "barButtonItemClicked:")
Swift 3.1 Update
斯威夫特 3.1 更新
UIBarButtonItem(title: "title", style: .plain, target: self, action: #selector(barButtonItemClicked))
回答by rvg
Swift 2 answer
斯威夫特 2 个回答
You could just add the following to viewDidLoad
:
您可以将以下内容添加到viewDidLoad
:
// Some text
self.barButtonItemClicked.title = "Right Button!"
OR
或者
// A Unicode Gear Symbol
// See: http://graphemica.com/%E2%9A%99
self.barButtonItemClicked.title = "\u{2699}"
The ViewController.Swift
code below would set the name barButtonItemClicked
you used. I used a UIBarButtonItem to show how to set the title. But it is trivial to adapt it for a function.
ViewController.Swift
下面的代码将设置barButtonItemClicked
您使用的名称。我使用了 UIBarButtonItem 来展示如何设置标题。但是将它调整为一个函数是微不足道的。
import UIKit
class ViewController: UIViewController {
// Please make sure the line below is properly connected to your Storyboard!
@IBOutlet weak var barButtonItemClicked: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set the text of the NavBarButtonItem Title
self.barButtonItemClicked.title = "Right Button!"
// Gear Icon
// self.barButtonItemClicked.title = "\u{2699}"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by Manu
In my case, I wanted to toggle between the Edit|Done.
However, I couldn't use the leftBarButtonItem because I already had another UIBarButtonItem
.
就我而言,我想在 Edit|Done 之间切换。但是,我无法使用 leftBarButtonItem,因为我已经有了另一个UIBarButtonItem
.
What I did is the following:
我所做的是以下内容:
1- Create @IBOutlet weak var edit: UIBarButtonItem!
1- 创建 @IBOutlet weak var edit: UIBarButtonItem!
2- Then a variable to hold the state: var isEditingMode = false
2-然后是一个保存状态的变量: var isEditingMode = false
3- Now in the viewDidLoad
:
3- 现在在viewDidLoad
:
override func viewDidLoad() {
…
self.edit.action = #selector(self.toogleEditor(_:))
self.edit.title = "Edit"
self.setEditing(isEditingMode, animated: true)
…
}
I initialize the edit.action Selector to my custom function toogleEditor
. I want to be able to change the title whenever an action occur.
我将 edit.action Selector 初始化为我的自定义函数toogleEditor
。我希望能够在发生操作时更改标题。
4- Create an IBAction:
4- 创建一个 IBAction:
@IBAction func toogleEditor(sender: AnyObject) {
if isEditingMode
{
isEditingMode = false
self.edit.title = "Edit"
}
else
{
isEditingMode = true
self.edit.title = "Done"
}
self.setEditing(isEditingMode, animated: true)
}
This function is triggered each time the user click the UIBarItemButton
.
The only thing to do is use the setEditing(…)
to change the behaviour of the UITableViewController
.
每次用户单击 时都会触发此功能UIBarItemButton
。唯一要做的就是使用setEditing(…)
来改变 的行为UITableViewController
。
回答by Joel
Swift 4:
斯威夫特 4:
@IBOutlet var saveButton: UIBarButtonItem!
saveButton.title = "Saved"
回答by RyanTCB
from the docs
从文档
init(title title: String?,
style style: UIBarButtonItemStyle,
target target: AnyObject?,
action action: Selector)