Code_Snippet

Thursday Tech Tip – iOS SubViews

Today’s tip involves how to create custom view controllers in Xcode 5. Creating custom views that leverage multiple custom subviews such as tables and collections is a great way of presenting information to users. However, it is not so straight forward for new iOS developers to master the art of instantiating subviews within a standard view. This approach is especially frustrating when you try to load data into a UICollectionView dynamically from a REST API call.

Tags in XcodeThe trick I have found is to use the “Tag” method in StoryBoard builder (see to the right). After adding a unique tag value to your UICollectionView object, just reference the same tag value in the method you use to get data for your collectionView. Below is an example refresh statement where I create a reference to a UICollectionView that is set to the tag value for the UICollectionView I made in Storyboard Builder. I then reload the view at the end of my method in which I get images via a REST API request.

UICollectionView *myCollectionView = (UICollectionView *)[self.view viewWithTag:1234];
        [myCollectionView reloadData];

This is a simple enough tip, but I burned too many hours trying to figure this solution out. I really wanted to create a custom view that was not a predefined view type Xcode Object library. A lot of the coolest apps out there use custom views but I couldn’t find a worthwhile solution that answered my specific question while attempting to do the same myself. So, here you go, I hope it saves you time.

Leave a Reply