Last active
November 5, 2015 19:14
-
-
Save prasadpamidi/a024c9f1970ae417baca to your computer and use it in GitHub Desktop.
Snippet for using enums instead strings for Storyboard segues - Approach to prevent crashes we often face with using strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIStoryBoard+Extensions.swift | |
// | |
// Created by Prasad Pamidi on 7/29/15. | |
// Copyright (c) 2015. All rights reserved. | |
// | |
import UIKit | |
enum SegueIdentifier:String { | |
case LogIn = "logInViewSegue" | |
case Home = "homeViewSegue" | |
case Profile = "profileViewSegue" | |
case Search = "searchViewSegue" | |
case Signature = "signatureViewSegue" | |
} | |
extension UIStoryboard { | |
class func mainStoryBoard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())} | |
class func loginController() -> LogInViewController? { | |
return mainStoryBoard().instantiateViewControllerWithIdentifier("LogInViewController") as? LogInViewController | |
} | |
} | |
// UIViewController+Extensions.swift | |
extension UIViewController { | |
func performSegueWithIdentifier(identifier: SegueIdentifier, sender: AnyObject?) { | |
self.performSegueWithIdentifier(identifier.rawValue, sender: self) | |
} | |
} | |
//Sample usecase | |
self.performSegueWithIdentifier(.LogIn, sender: self) | |
self.performSegueWithIdentifier(.Search, sender: self) | |
self.performSegueWithIdentifier(.Profile, sender: self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wouldn't it be cool to have an @IBDesignable extension that can provide us with a list of SegueIdentifiers to choose from?