Reputation: 992
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
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)
Upvotes: 7
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
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