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 full-size slide

  2. I spent a lot
    of time hating
    on Storyboards

    View full-size slide

  3. So, on any given
    NSCoder Night…

    View full-size slide

  4. “Storyboards are
    awesome!”
    1
    Abbie

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. But we decide to
    keep the Storyboards

    View full-size slide

  10. I cry, again

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. Use Multiple Storyboards

    View full-size slide

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

    View full-size slide

  15. Dynamic Prepare For Seque

    View full-size slide

  16. 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 full-size slide

  17. 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 full-size slide

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

    View full-size slide

  19. Multiple Segues to a Single View

    View full-size slide

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

    View full-size slide

  21. 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 full-size slide

  22. Naming Conventions

    View full-size slide

  23. 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 full-size slide

  24. Nib-backed Views

    View full-size slide

  25. @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 full-size slide

  26. So, It hasn't been so bad

    View full-size slide

  27. Keep an open mind

    View full-size slide

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

    View full-size slide