以编程方式添加导航按钮执行

时间:2015-07-26 01:05:12

标签: ios swift2 xcode7 rightbarbuttonitem

我正在尝试以编程方式自定义导航栏按钮项目...我只是在这里遇到一些问题,我相信这是一个简单的解决方法。我想让它成为现在的默认添加按钮,但是将来还会想要使条形按钮项目成为我创建的自定义图标。所以我想我想知道如何以编程方式将导航栏按钮更改为默认样式和自定义图标......谢谢!

sscanf

1 个答案:

答案 0 :(得分:0)

以下是使用默认添加按钮执行此操作的方法:

    let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "uploadButtonClicked")
    self.navigationItem.rightBarButtonItem = button

以下是以编程方式添加自定义图像的方法:

    let customImage = UIImage(named: "customButtonImage")
    let customButton = UIBarButtonItem(image: customImage, style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked:")
    self.navigationItem.rightBarButtonItem = customButton

要执行segue,您可以执行以下操作:

@IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

或者,如果你想从那里去另一个故事板,那么你的IBAction将如下所示:

@IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
    let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
    let nc = storyboard.instantiateInitialViewController() as! UINavigationController
    let vc = nc.viewControllers.first as! YourViewController
相关问题