xcode 如何将工具栏按钮添加到我的导航控制器中的工具栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25943951/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:37:44  来源:igfitidea点击:

How to add ToolBar button to Tool Bar in my Navigation Controller

iosxcode

提问by n179911

I am using XCode I have added a Toolbar to my Navigation Controller. And I want to add Bar Buttons to that toolBar. In my storyboard, I try control drag 'Bar Button Item' to the tool bar, but that does not work, nothing gets added.

我正在使用 XCode 我在我的导航控制器中添加了一个工具栏。我想向那个工具栏添加条形按钮。在我的故事板中,我尝试控制将“Bar Button Item”拖动到工具栏,但这不起作用,没有添加任何内容。

So can 'Bar Button Item' to the tool bar?

那么可以将“Bar Button Item”添加到工具栏吗?

Thank you.

谢谢你。

Update:

更新:

I add my own UINavigationController, bind the toolbar from storyboard to my class as 'mytoolbar'. And did the answer code in swift, But when I run it on simulator, I don't see any tool bar item.

我添加了自己的 UINavigationController,将故事板中的工具栏作为“mytoolbar”绑定到我的类。并快速完成了答案代码,但是当我在模拟器上运行它时,我没有看到任何工具栏项目。

class AppNavigationController: UINavigationController {


    @IBOutlet weak var mytoolbar: UIToolbar!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
        var items = [AnyObject]()

        items.append(rightButton)
        mytoolbar.items = items

    }

    func method() {
        println("Something cool here")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

回答by 2ank3th

For Swift:

对于斯威夫特:

 let item1 = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: "touchPickImage")

 let item2 = UIBarButtonItem(title: "Delete", style: .Plain, target: self, action: "deleteImage")

 toolBar.setItems([item], animated: true)

you can get reference for tool bar either from view controller or you drag it and then add.

您可以从视图控制器获取工具栏的参考,也可以拖动它然后添加。

回答by Ryan Kreager

You may want to go with a programmatic solution here:

您可能想在此处使用程序化解决方案:

UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Title 1"
                                                              style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(buttonClick1:)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"Title 1"
                                                              style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(buttonClick2:)];
NSMutableArray *myButtonArray = [[NSArray alloc] initWithObjects:button1, button2, nil];
toolBar.items = myButtonArray;

回答by Paul G

Try setting your bar button items, then using:

尝试设置您的条形按钮项目,然后使用:

self.setToolbarItems([UIBarButtonItem], animated: false)

self.setToolbarItems([UIBarButtonItem], 动画:false)