简体   繁体   English

如何为 UIBarButtonItem 设置标题?

[英]How do I set title for UIBarButtonItem?

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
}

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))

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.下面的ViewController.Swift代码将设置您使用的名称barButtonItemClicked I used a UIBarButtonItem to show how to set the title.我使用了 UIBarButtonItem 来展示如何设置标题。 But it is trivial to adapt it for a function.但是将它调整为一个函数是微不足道的。

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.
    }

}

Swift 4:斯威夫特 4:

@IBOutlet var saveButton: UIBarButtonItem!

saveButton.title = "Saved"

In my case, I wanted to toggle between the Edit|Done.就我而言,我想在 Edit|Done 之间切换。 However, I couldn't use the leftBarButtonItem because I already had another UIBarButtonItem .但是,我无法使用 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 .我将 edit.action Selector 初始化为我的自定义函数toogleEditor I want to be able to change the title whenever an action occur.我希望能够在发生操作时更改标题。

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 .每次用户单击UIBarItemButton时都会触发此函数。 The only thing to do is use the setEditing(…) to change the behaviour of the UITableViewController .唯一要做的就是使用setEditing(…)来改变UITableViewController的行为。

If anyone wondering how to do it from the storyboard: Try to edit the "Title" attribute in "Attributes Inspector" as shown below:如果有人想知道如何从故事板中做到这一点:尝试编辑“属性检查器”中的“标题”属性,如下所示: 在此处输入图片说明

from the docs从文档

init(title title: String?,
 style style: UIBarButtonItemStyle,
target target: AnyObject?,
action action: Selector)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM