Bogdan Bogdanov
Bogdan Bogdanov

Reputation: 992

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

Upvotes: 2

Views: 8646

Answers (3)

simons
simons

Reputation: 2400

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

Upvotes: 7

Zell B.
Zell B.

Reputation: 10286

If you want to do this via storyboard connect your view controllers in storyboard with segue (drag from first view controller to second one, it doesn't mean to drag from button only) name that segue "oc" then in button action perform segue like following

performSegueWithIdentifier("oc", sender: but)

Upvotes: 0

Özgür Ersil
Özgür Ersil

Reputation: 7013

you can use dynamic popup views like that

 let vc = storyboard.instantiateViewControllerWithIdentifier("myPopupView") as! myPopupViewViewController

 self.presentViewController(vc, animated: true, completion: nil)

Upvotes: 2

Related Questions