Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - UIStoryboardSegue pop up ViewController programatically

I have UIButton attached to navigationBar programatically:

catButton.addTarget(self, action: "oc:", forControlEvents: .TouchUpInside)
var catBarButton:UIBarButtonItem = UIBarButtonItem(customView: catButton)
self.navigationItem.setRightBarButtonItem(catBarButton, animated: false)

when function oc() is triggered I want to popover view controller

let segue = UIStoryboardSegue(identifier: "oc", source: self, destination: MDComDBCatsTVC())
prepareForSegue(segue, sender: but)

but this doesn't open MDComDBCatsTVC.. How to do it programatically because I can't drag from button in my storyboard because my button is added programatically

like image 829
Bogdan Bogdanov Avatar asked Jan 18 '26 06:01

Bogdan Bogdanov


1 Answers

I can't drag from button in my storyboard because my button is added programatically

You can create the segue in the storyboard by dragging from the view controller icon to the target view controller. This segue can then be used programmatically.

Click on the segue to assign an identifier in the Attributes inspector panel. The segue can then be accessed programmatically using this identifier.

Xcode will insist that popover segues have an anchor. The error message is “Popover segue with no anchor”. This can be resolved in the Attributes inspector - the Anchor property. Drag from the circle to your view.

In the action for your programmatically created button you will then be able to access the segue using eg:

func buttonAction(sender:UIButton!)
{
    performSegueWithIdentifier("popoversegue", sender: self)
}

( ... or more likely something more robust using if let etc)

Attributes inspector

like image 128
simons Avatar answered Jan 19 '26 20:01

simons