performSegue 以编程方式添加导航按钮

performSegue with programmatically added Navigation button

我正在尝试以编程方式自定义导航栏按钮项目...我只是遇到了一些问题,我相信这很容易解决。我现在想让它成为默认的添加按钮,但将来也想让栏按钮项目成为我创建的自定义图标。所以我想我想知道如何以编程方式将导航栏按钮更改为默认样式和自定义图标的两种方法...谢谢!

override func viewDidAppear(animated: Bool) {

    let rightbutton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked")

    navigationItem.rightBarButtonItem = rightbutton


}

以下是使用默认添加按钮的方法:

    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