【问题标题】:How can I get a navigation bar button to segue?我怎样才能让导航栏按钮继续?
【发布时间】:2015-08-06 02:35:28
【问题描述】:

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

我的理解是,如果我想以编程方式创建此 segue,我需要将操作设为“performSegueWithidentifier”方法。这是我所拥有的:

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

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

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

这是我用于 segue 的代码:

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

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

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

【问题讨论】:

    标签: ios swift uinavigationcontroller


    【解决方案1】:
    1. UIViewController 之间创建segue(右键单击firstViewController 并将其拖到secondviewcontroller

    1. segue 指定标识符名称

    1. 使用 segue 名称执行 segue

    现在为了使用UIBarButtonItem 执行segue,将viewDidLoad 方法中的以下代码添加到firstViewController

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "navigateToNextViewController")
        // Do any additional setup after loading the view, typically from a nib.
    }
    

    现在创建navigateToNextViewController 方法并从该方法执行segue

    func navigateToNextViewController(){
        self.performSegueWithIdentifier("goNext", sender: self)
    }
    

    【讨论】:

    • 知道是什么原因造成的吗? 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 ...
    • @Brandon 找不到selector参数中指定的方法
    • @Brandon 你怎么称呼那个 segue 方法?
    【解决方案2】:

    您可以从 UIBarButtonItem 控制 + 拖动到 Storyboard 中的 UIViewController(或其他类型的控制器)。

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

    let plusButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "plusBttnTouched:")
    
    func plusBttnTouched(sender: UIBarButtonItem) {
    
        performSegueWithIdentifier(identifier: "segueNameHere", sender: self)
    
    }
    

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

    故事板:

    代码:

    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)
        })
    
    }
    
    }
    

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

    【讨论】:

    • 我试过没有成功,可能是因为我没有将“sender”作为“plusBttnTouched”的参数。您能在这里向我解释一下“发送者”的角色吗?另外,你能解释一下通过界面使用它和通过代码使用它的区别吗?当我尝试将它拖到导航栏上时,它没有出现。
    • 对不起,我的意思是控制拖动
    • 收到以下错误(我使用的代码放在原始帖子中):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 ...
    • 删除 : 你调用 goToSegue 的地方
    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2022-01-24
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多