Josh Lopez Josh Lopez - 1 year ago 85
iOS Question

Swift segue to xib file programatically

I cant seem to figure this out. I am new to swift. i need to load a xib file from a navigation bar button item. I have to do this outside the storyboard because the same button item changes depending if the user is logged in or not. It reads "Login/Register" if a user is not logged in and goes to a login xib file and if a user is logged in it reads "Add Story" and goes to a add story xib file. Thank you for your time I really appreciate it.


First, use an IBOutlet to create a var of type UIBarButtonItem and an action method for the button. You can then access the button programmatically to update it's label (logged in/out) when the view appears. You can do both of these by control-dragging (or right-click drag) from the button in the storyboard to the swift file.

In the storyboard, setup two segues that go to different views (i.e. "LoginSegueIdentifier" and "AddStorySegueIdentifier". Inside the action method, check if the user is logged in or not. Depending on the result, perform a segue with an the identifier matching the segue identifier.

To Login...

self.performSegueWithIdentifier("LoginSegueIdentifier", sender: self)

To Add story

self.performSegueWithIdentifier("AddStorySegueIdentifier", sender: self)

Hope this helps. Let me know if you have any questions.

UPDATE: To clarify, this is pretty common (at least in my experience) and you can definitely achieve this with storyboards.