Search code examples
iosswiftswift2segueuistoryboardsegue

performSegueWithIdentifier not showing view controller as expected


I have programatically created a button called 'menu' which was changed to a UIBarButtonItem called 'menuButton'. When the button is clicked I want to call a segue using the performSegueWithIdentifier.

Universal.swift

struct UniversalStatic {
    static let data = Universal()
}

public class Universal {
    let menuButton : UIBarButtonItem
    let menu : UIButton

init(){
    menu =  UIButton(type: UIButtonType.Custom)
    menu.setImage(UIImage(named: "menu.png"), forState: UIControlState.Normal)
    menu.frame = CGRectMake(0, 0, 25, 20)
    menuButton = UIBarButtonItem(customView: menu)
}

HomeViewController.swift

class HomeViewController: UIViewController {

    func goToMenuVC() {
    self.performSegueWithIdentifier("test", sender: self)
    }

    override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
        print("called")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        UniversalStatic.data.menu.addTarget(self, action: "goToMenuVC", forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.leftBarButtonItem = UniversalStatic.data.menuButton
    }

func viewDidAppear() {
    UniversalStatic.data.menu.addTarget(self, action: "goToMenuVC", forControlEvents: UIControlEvents.TouchUpInside)
    self.navigationItem.leftBarButtonItem = UniversalStatic.data.menuButton
}

The print statement is printed so i know the 'performSegueWithIdentifier' is been called however the show segue is bene actioned.

I have attached a screenshot of the segue identifier. enter image description here


Solution

  • Try to add:

    super.performSegueWithIdentifier(identifier, sender: sender)
    

    In your performSegueWithIdentifier method.