我怎样才能让导航栏按钮继续? [英] How can I get a navigation bar button to segue?

查看:54
本文介绍了我怎样才能让导航栏按钮继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向 UIViewController 的 UINavigationController.在那个 UIViewController 中,我希望导航项的右侧按钮是一个 .Add UIBarButtonItem,它会转到另一个名为nextScene"的场景.

I have a UINavigationController that points to a UIViewController. In that UIViewController, I want the right button of the navigationitem to be a .Add UIBarButtonItem which segues to another scene called "nextScene".

据我所知,如果我想以编程方式创建此转场,我需要将操作设为performSegueWithidentifier"方法.这是我所拥有的:

It's my understanding that I need to have the action be the "performSegueWithidentifier" method if I want to create this segue programmatically. Here is what I have:

let plusButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "performSegueWithIdentifier:")
self.navigationItem.setRightBarButtonItem(plusButton, animation: true)

进入另一个名为nextScene"的场景的正确语法是什么?我的 performSegueWithidentifier 方法应该如何处理这个问题?

What is the proper syntax for getting to another scene called "nextScene"? How should my performSegueWithidentifier method handle this?

收到以下错误:无法识别的选择器发送到实例... 2015-08-06 07:57:18.534 ..[...] *** 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[... goToSegue:]: 无法识别的选择器发送到实例....

Getting the following error: unrecognized selector sent to instance ... 2015-08-06 07:57:18.534 ..[...] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[... goToSegue:]: unrecognized selector sent to instance ....

这是我用于转场的代码:

Here is the code I'm using for the segue:

let plusButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "goToSegue:")`

self.navigationItem.setRightBarButtonItem(plusButton, animated: true) }

func goToSegue() {
    performSegueWithIdentifier("segueName", sender: self)
}

推荐答案

您可以直接从 UIBarButtonItem 控制并拖动到故事板中的 UIViewController(或其他类型的控制器).

You can just control + drag from your UIBarButtonItem to the UIViewController (or other type of controller) in your Storyboard.

如果你想通过代码来完成,你需要使用目标类中可以处理它的方法来备份你的动作调用.performSegueWithIdentifier 是视图控制器的默认方法,因此我会调用另一个函数,然后调用 performSegueWithIdentifier,如下所示:

If you want to do it through code you'll need to backup your action call with a method in your target class that can handle it. performSegueWithIdentifier is a default method for your view controller so I would call another function that then calls performSegueWithIdentifier, like this:

let plusButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "plusBttnTouched:")

func plusBttnTouched(sender: UIBarButtonItem) {

    performSegueWithIdentifier(identifier: "segueNameHere", sender: self)

}

这是一个更新的代码示例:

Here's an updated code example:

故事板:

代码:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Create bar button item

    let plusButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: self, action: Selector("plusBttnTouched:"))

    self.navigationItem.rightBarButtonItems = [plusButton]

    // Do any additional setup after loading the view, typically from a nib.
}

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

//MARK: - Actions

func plusBttnTouched(sender: UIBarButtonItem) {

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        self.performSegueWithIdentifier("plusViewController", sender: self)
    })

}

}

在方法参数中使用发送者允许您访问方法中定义类型的实例.当你在选择器的末尾添加一个 : 时,你说你想要这个,这不是必需的.

Using a sender in your method parameter allows you to access the instance of the defined type within your method. You say you want this when you add a : to the end of your selector, it is not required.

这篇关于我怎样才能让导航栏按钮继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆