Skip to content

Instantly share code, notes, and snippets.

@prasadpamidi
Last active November 5, 2015 19:14
Show Gist options
  • Save prasadpamidi/a024c9f1970ae417baca to your computer and use it in GitHub Desktop.
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
//
// 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)
@prasadpamidi
Copy link
Author

Wouldn't it be cool to have an @IBDesignable extension that can provide us with a list of SegueIdentifiers to choose from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment