The iOS Dev Diary

The iOS Dev Diary

By Jay VersluisTechnology
Download on the App Store

The iOS Dev Diary episodes

  • How to build a UICollectionView in iOS 8

    In this episode I’ll show you how to build a UICollectionView from scratch in Xcode 6. The class is available for both iPhone and iPad since iOS 6. If you know how to build a UITableView then building a UICollectionView will be familiar to you.

    I’ll start with a single view application, delete the ViewController class and start fresh with a UICollectionViewController. Next I’ll add a custom class for the UICollectionViewController and UICollectionViewCell and then we’ll hook it up in the storyboard.

    By the end we’ll have a simple Collection View App which allows multiple selections. I’m going to use this project to build on with other features in the future.

    Custom CollectionViewController Class

    The template provides a few good starting points, but they need to be changed to work. First there’s the cell’s reuse identifier, conveniently added as a static at the top of the implementation file. It’s there so we only need to change this once in the file. Replace it with your own, and remember to make the same change in the storyboard:

    static NSString * const reuseIdentifier = @"Cell";

    Next up is the viewDidLoad method. To make dequeuing cells easier, Apple have provided a registerClass method. If you don’t add your custom cell here, nothing will appear when you run the app. I found that simply commenting out the line works just as well.

    The reason they provide this is so that the dequeueCellWithIdendifier method knows which custom cell class to instantiate (prior to iOS 6 it returned nil, but that check is no longer necessary).

    I’m also adding multiple selections here, something that cannot be done in the storyboard.

    - (void)viewDidLoad {
    [super viewDidLoad];
    // EVIL: Register your own cell class (or comment this out)
    [self.collectionView registerClass:[YourCustomCell class] forCellWithReuseIdentifier:reuseIdentifier];
    // allow multiple selections
    self.collectionView.allowsMultipleSelection = YES;
    self.collectionView.allowsSelection = YES;
    }

    UICollectionViewDataSource

    Much like with UITableViews, we need to provide the number of sections, as well as the number of items in each section:

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    }
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cellData.count;
    }

    If you don’t provide the sections method, it is assumed that you have one section. Usually we’d have some data and would return a count of how many items we have rather than fixed values here.

    We also need to provide what’s in each cell. Here we can add data to labels, populate UIImageViews and many other things our collection view cells may need:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.cellData objectAtIndex:indexPath.row];
    return cell;
    }

    UICollectionViewCell

    With UITableViews there were four styles of table cells we could choose from out of the box. A collection view cell on the other hand is completely blank, and we’re expected to provide everything inside it. This means we need a custom UICollectionViewCell class for our project.

    Anything we drag into the prototype cell in the storyboard can be hooked up to that custom class and the configured in the above cellForItemAtIndexPath method. Make sure any outlets are defined in the cell’s header file.

    Cell Selections

    Collection view cells have several views layered on top of each other. At the bottom is the backgroundView and the selectedBackgroundView. These are not configured by default, but if we add our own views here, the cell knows how display selections.

    Above the background/selectedBackgroundView is the contentView, and on top of the contentView is where we can add out own outlets (like labels and images). If we leave the contentView’s background colour transparent, the views underneath will be visible, and hence selections are visible too.

    Here’s how to configure our custom cell with two colours for selection and deselection. I’m doing this in awakeFromNib, which is called as soon as our cell is instantiated:

    - (void)awakeFromNib {
    // standard background (deselected)
    UIView *bgView = [[UIView alloc]initWithFrame:self.bounds];
    self.backgroundView = bgView;
    self.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue"]];
    // selected background
    UIView *selectedView = [[UIView alloc]initWithFrame:self.bounds];
    self.selectedBackgroundView = selectedView;
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pink"]];
    }

    Demo Project

    The code I’m writing here is available as a demo project on GitHub:

    • https://github.com/versluis/Collection-View
    • Watch the full course in one convenient playlist:
      Catch this episode on my iOS Dev Diary Podcast:

      27 min
    • How to create an Unwind Segue in iOS 8

      The Unwind Segue was introduced in iOS 6 to make retrieving data from a dismissed view controller easier. A regular Segue allows us to send data from one view controller to another, but it’s not easy to bring data back if the user has changed or added details in that view controller. That’s where an software like the one at Paxata comes in.

      Part 1: Code

      Here’s how to create one. I’m assuming we have two view controllers at our disposal, ViewController1 has initiated a standard segue, maybe passing some data. The user then changes the data, and upon saving the data, ViewController2 is dismissed and we’re going back to ViewController1.

      So in ViewController1 we’ll add a method we can use as an Unwind Segue, which we later hook up in the storyboard to the Exit object. Here’s that method:

      - (IBAction)backToTheStart:(UIStoryboardSegue *)segue {
      // grab a reference
      ViewController2 *viewController2 = segue.sourceViewController;
      // access public properties from ViewController2 here
      }

      It doesn’t matter what the method is called, just as long as it resides in ViewController1, in which you’ve imported the ViewController2.h file. What DOES matter however that our method is an IBAction and takes a UIStoryboardSegue as a parameter – otherwise Interface builder won’t recognise it, and you won’t be able to drag to the Exit object later.

      Meanwhile, in ViewController2, we can make use of the following method which is often provided as a stub in new view controller classes:

      - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      // do any preparation here when the segue is called
      }

      prepareForSegue is called just before the unwind segue (or any segue for that matter) is about to begin. You may want to read out a text field or any other values and add it to the object or variable that ViewController1 needs access to.

      Part 2: Hooking up the Storyboard

      With our code in place, control-drag from the button you’d like to use for initiating the segue. You can hook up multiple buttons to the same unwind segue.

      Control-drag each button to the Exit object (in ViewController2):

      As soon as the button is pressed, the unwind code in ViewController1 is executed and ViewController2 is dismissed.

      Demo Project

      I’ve put together a quick demo project on GitHub which is the code I’m creating the screencast above:

      • https://github.com/versluis/Unwind-Segue
      • Further Reading
        • https://developer.apple.com/library/ios/technotes/tn2298/_index.html
        • Watch the full course in one convenient playlist:
          Catch this episode on my iOS Dev Diary Podcast:

          20 min
        • How to speak Core Data

          In this episode core data I’ll talk you through the lingo of Core Data: those scary classes and expressions that you’ll frequently come across. In fact, this is the start of a new series:

          Core Data Nuggets are bite-sized chunks about the framework. Dip in and out or watch them all in a row and learn how this super complicated framework works and what it has to offer.

          Don’t get overwhelmed by Core Data: it wants to help – it’s just not designed with humans in mind.

          As always, enjoy!

          Watch the full course in one convenient playlist:
          Catch this episode on my iOS Dev Diary Podcast:

          27 min
        • Creating an In-App Purchase in iOS 7 – Part 6: Restoring

          And finally in the last part I’ll show you how to restore purchases.

          Note that you must implement a restore option for non-consumable products or the review team will reject your app.

          Demo Project

          You can download the full source code of what I’m building in this screencast on GitHub:

          • https://github.com/versluis/In-App-Purchases/
          • Further Reading

            I’m following my earlier two articles almost to the letter, here they are for reference:

            • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
            • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
            • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
            • Watch the full course in one convenient playlist:
              Catch this episode on my iOS Dev Diary Podcast:

              9 min
            • Creating an In-App Purchase in iOS 7 – Part 5: Testing

              In this part we’ll see our hard work comes to life on a real device while we’re performing our purchase in the App Store Sandbox.

              Demo Project

              You can download the full source code of what I’m building in this screencast on GitHub:

              • https://github.com/versluis/In-App-Purchases/
              • Further Reading

                I’m following my earlier two articles almost to the letter, here they are for reference:

                • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
                • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
                • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
                • Watch the full course in one convenient playlist:
                  Catch this episode on my iOS Dev Diary Podcast:

                  18 min
                • Creating an In-App Purchase in iOS 7 – Part 4: The Purchase

                  Learn how to make the purchase in this part of our series about in-app purchases.

                  Demo Project

                  You can download the full source code of what I’m building in this screencast on GitHub:

                  • https://github.com/versluis/In-App-Purchases/
                  • Further Reading

                    I’m following my earlier two articles almost to the letter, here they are for reference:

                    • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
                    • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
                    • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
                    • Watch the full course in one convenient playlist:
                      Catch this episode on my iOS Dev Diary Podcast:

                      4 min
                    • Creating an In-App Purchase in iOS 7 – Part 3: StoreKit Observer

                      In this part we’ll setup the observers needed to react to responses from the App Store.

                      Demo Project

                      You can download the full source code of what I’m building in this screencast on GitHub:

                      • https://github.com/versluis/In-App-Purchases/
                      • Further Reading

                        I’m following my earlier two articles almost to the letter, here they are for reference:

                        • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
                        • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
                        • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
                        • Watch the full course in one convenient playlist:
                          Catch this episode on my iOS Dev Diary Podcast:

                          8 min
                        • Creating an In-App Purchase in iOS 7 – Part 2: Custom Shop Class

                          In this part I’ll talk you through building a custom shop class and how to make “first contact” with the App Store.

                          Demo Project

                          You can download the full source code of what I’m building in this screencast on GitHub:

                          • https://github.com/versluis/In-App-Purchases/
                          • Further Reading

                            I’m following my earlier two articles almost to the letter, here they are for reference:

                            • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
                            • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
                            • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
                            • Watch the full course in one convenient playlist:
                              Catch this episode on my iOS Dev Diary Podcast:

                              20 min
                            • Creating an In-App Purchase in iOS 7 – Part 1: Setup

                              In this 7-part screencast series I’ll show you how to create an In-App Purchase in iOS 7 with Xcode 5.1.

                              The course will run you through everything from setting up your product in iTunes Connect, creating a custom shop class for easy re-use, making “first contact” with the App Store and how to deal with its responses. I’ll explain the overall concept in Part 1.

                              I’ll describe the overall concept in this part, and how to setup your your app for use with In-App purchases. We’ll setup a new App ID in Member Center and then create a product in iTunes Connect.

                              Demo Project

                              You can download the full source code of what I’m building in this screencast on GitHub:

                              • https://github.com/versluis/In-App-Purchases/
                              • Further Reading

                                I’m following my earlier two articles almost to the letter, here they are for reference:

                                • http://pinkstone.co.uk/how-to-create-a-single-in-app-purchase-in-ios-7/
                                • http://pinkstone.co.uk/how-to-restore-your-single-in-app-purchase-in-ios/
                                • https://developer.apple.com/library/ios/technotes/tn2259/_index.html
                                • Watch the full course in one convenient playlist:
                                  Catch this episode on my iOS Dev Diary Podcast:

                                  16 min
                                • How to use Popovers on iPad – Part 3: Image Picker

                                  In this series I’ll show you how to create Popovers on iPad. They’re fairly easy to create once you get the hang of the inner workings of the UIPopoverController.

                                  I’ll show you how to create basic Popover in code and in your Storyboard, and we’ll discuss how you can retrieve data from a Popover when it’s dismissed. We’ll do this with a simple UIDatePicker. In the last video I’ll demonstrate how you can pick images from the camera roll using the UImagePickerController with a Popover – which is how you’re meant to do it on iPad.

                                  The series contains three videos in total:

                                  • Part 1 deals with the initial code and class etup
                                  • Part 2 deals with setting up the UIStoryboard and connect our code to it
                                  • and in Part 3 we’ll implement a UIImagePickerController to demonstrate a use case
                                  • Enjoy!

                                    Get the code for this project on GitHub
                                    • https://github.com/versluis/Popovers
                                    • Watch the full course in one convenient playlist:
                                      Catch this episode on my iOS Dev Diary Podcast:

                                      11 min

                                    About The iOS Dev Diary

                                    From the publisher's feed

                                    Fantastic Adventures in Objective-C