Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How I Learned to Quit Worrying and Love Storyboards

How I Learned to Quit Worrying and Love Storyboards

For a long time, I've been convinced that the only way to build iOS applications. Recently I've made the switch to use Storyboards for a project. At CocoaHeads May in Durham, NC I shared some of the things we've been doing to stay productive while working with something that was new to us.

Josh Johnson

May 22, 2014
Tweet

More Decks by Josh Johnson

Other Decks in Programming

Transcript

  1. How I Learned to
    Stop Worrying and
    Love the Storyboard
    Josh Johnson · @jnjosh · jnjosh.com

    View Slide

  2. I spent a lot
    of time hating
    on Storyboards

    View Slide

  3. So, on any given
    NSCoder Night…

    View Slide

  4. “Storyboards are
    awesome!”
    1
    Abbie

    View Slide

  5. “That's crazy, I'll
    just write the code!"
    1
    Me

    View Slide

  6. “No really, you
    should look again…”
    1
    Abbie

    View Slide

  7. “Why do people hate
    writing code?”
    1
    Me

    View Slide

  8. So, then we get a
    client project that
    uses Storyboards

    View Slide

  9. I cry

    View Slide

  10. But we decide to
    keep the Storyboards

    View Slide

  11. I cry, again

    View Slide

  12. Spoiler: It hasn't
    been so bad…

    View Slide

  13. A partial list of
    things we've done
    to work with
    Storyboards.

    View Slide

  14. Use Multiple Storyboards

    View Slide

  15. View Slide

  16. Crossing to another Storyboard
    UIViewController *mainViewController =
    [[UIStoryboard storyboardWithName:@"MainWorkflow" bundle:nil]
    instantiateInitialViewController];
    [self presentViewController:mainViewController
    animated:YES
    completion:nil];

    View Slide

  17. Dynamic Prepare For Seque

    View Slide

  18. This can get ugly…
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    NSString *identifier = segue.identifier;
    if ([identifier isEqualToString:@"FriendsToFacebookFriends"]) {
    // …
    } else if ([identifier isEqualToString:@"FriendsToTwitterFriends"]) {
    // …
    } else if ([identifier isEqualToString:@"FriendsToContacts"]) {
    // …
    }
    }

    View Slide

  19. Dynamic Method Dispatch!
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    [self twt_prepareForSegue:segue sender:sender];
    }
    ///
    - (void)twt_prepareForFriendsToFacebookFriendsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;
    - (void)twt_prepareForFriendsToTwitterFriendsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;
    - (void)twt_prepareForFriendsToContactsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;

    View Slide

  20. UIViewController+TWTPrepareForSegue.h
    https://github.com/twotoasters/Toast
    pod 'TWTToast/UIKit/PrepareForSegue', '~> 0.8'

    View Slide

  21. Multiple Segues to a Single View

    View Slide

  22. View Slide

  23. Manually Performing the Segue
    - (void)itemSelected:(TWTItem *)item
    {
    if ([self shouldPerformSegueWithIdentifier:item.segueIdentifier
    sender:self]) {
    [self performSegueWithIdentifier:item.segueIdentifier
    sender:self];
    }
    }

    View Slide

  24. Remember Dynamic Method Dispatch?
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    [self twt_prepareForSegue:segue sender:sender];
    }
    ///
    - (void)twt_prepareForFriendsToFacebookFriendsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;
    - (void)twt_prepareForFriendsToTwitterFriendsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;
    - (void)twt_prepareForFriendsToContactsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;

    View Slide

  25. Naming Conventions

    View Slide

  26. Name your Segue Identifiers
    consistently
    static NSString *const kInviteSourceToContactsSegueIdentifier = @"InviteSourceToContacts";
    static NSString *const kInviteSourceToFacebookSegueIdentifier = @"InviteSourceToFacebook";
    static NSString *const kInviteSourceToTwitterSegueIdentifier = @"InviteSourceToTwitter";
    ///
    - (void)twt_prepareForInviteSourceToContactsSegue:(UIStoryboardSegue *)segue
    sender:(id)sender;

    View Slide

  27. Nib-backed Views

    View Slide

  28. @protocol TWTNibBackedView
    + (UINib *)nib;
    @end
    /// In TWTTestCollectionViewCell
    + (UINib *)nib
    {
    return [UINib nibWithNibName:NSStringFromClass(self) bundle:nil];
    }
    /// In TWTTestCollectionViewController -viewDidLoad
    [collectionView registerNib:[TWTTestCollectionViewCell nib]
    forCellWithReuseIdentifier:kCellIdentifier];

    View Slide

  29. So, It hasn't been so bad

    View Slide

  30. Keep an open mind

    View Slide

  31. Thanks! Questions?
    Josh Johnson · @jnjosh · jnjosh.com

    View Slide