如何工具栏按钮添加到工具栏在我的导航控制器(How to add ToolBar button to

2019-09-26 12:12发布

我使用的XCode我添加了一个工具栏,以我的导航控制器。 我想栏按钮添加到工具栏。 在我的故事板,我试图控制拖“栏按钮项目”到工具栏,但不工作,没有被添加。

所以“栏按钮项目”到工具栏?

谢谢。

更新:

添加我自己的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.
    }

}

Answer 1:

对于斯威夫特:

 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)

你可以得到无论是从视图控制器工具栏参考或拖动它,然后添加。



Answer 2:

你可能想要去与这里编程解决方案:

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;


Answer 3:

请尝试设置栏按钮的项目,然后使用:

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



文章来源: How to add ToolBar button to Tool Bar in my Navigation Controller
标签: ios xcode