Reputation: 25
I´m trying to make a new custom navigation bar button with my own image. That´s working fine, but I´m trying to open a new view controller when I press that button. Unfortunately that doesn´t work and I have no idea how I should do it.
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "Image.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
func clickButton(sender: UIBarButtonItem){}
That´s my code right now and its inside my viewDidLoad. My intention is to perform a show detail Segue comment. Can someone help me ?
Upvotes: 2
Views: 3171
Reputation: 809
why don't you just use a UIButton and assign it as bar button item as suggested in this thread:
UIBarButtonItem: target-action not working?
let button = UIButton(type: .Custom)
if let image = UIImage(named:"icon-menu.png") {
button.setImage(image, forState: .Normal)
}
button.frame = CGRectMake(0.0, 0.0, 30.0, 30.0)
button.addTarget(self, action: #selector(MyClass.myMethod), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
Upvotes: 1
Reputation: 54716
If you are using a Storyboard
, add a manual segue
to your ViewController
and give it an identifier. Then you have to call performSegue
in your custom navigationbar button's IBAction
.
func clickButton(sender: UIBarButtonItem){
self.performSegue(withIdentifier: "yourSegueIdentifier", sender: nil)
}
Upvotes: 0