我在viewDidLoad方法中设置navigationBarItem

var button = UIBarButtonItem(title: "Menu", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("goToMenu:"))
self.navigationItem.rightBarButtonItem = button


我有一个方法可以选择此按钮操作

func goToMenu() {
    var destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("menuBoard") as MenuViewController
    self.navigationController?.presentViewController(destinationViewController, animated: true, completion: nil)
}


但我有例外

goToMenu:]: unrecognized selector sent to instance 0x15678990'


我做错了什么?

最佳答案

修改代码为:

func goToMenu(sender: AnyObject) {
    var destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("menuBoard") as MenuViewController
    self.navigationController?.presentViewController(destinationViewController, animated: true, completion: nil)
}


原因是您的Selector("goToMenu:")寻找带有一个参数的方法,但是您的goToMenu实际上没有参数

07-24 19:31