Best Practices Example for iOS detail screen
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

In any programming language / platform there are best practices and patterns for doing things. And while I can find established examples for how to handle various things in iOS, I haven't found good examples of something that I really consider a pretty basic need.

That need is a good example of the master detail screens that are common in almost all applications, and especially line of business applications. I think it is important to focus on fundamentals and make sure apps are created using well defined and understood building blocks.

I have been able to find examples but they all seem to be more academic and not about minimizing real world effort, and maximizing understanding and consistency for new developers.

I really like RestKit and would prefer solutions that use it. I also really like NimbusKit. Outside of that I would prefer solutions that only used built in iOS functionality.

I'll definitely tip for anything I see that really impresses me, or looks like it would help in large projects that will have lots of lists and related detail screens.

You can see an example of one of the screens to be created below.

example detail screen

Feel free to tie to an existing API source like Twitter, etc.

This is really a simple thing, but with a big focus on quality. Again I could see giving a tip for really impressive solutions that go above and beyond the minimum.

@ChrisJCraft I'm not sure I fully understand what you want, could you please clarify? Are you looking for a better design your detail screen? or are you looking for something else?
alex over 6 years ago
Hi Alex, the goal is code only, but around generally in detail screens you have a list screen and a related detail screen. In my case I'm always doing projects that are master list screens, with related child detail screens. They always pull code from JSON restful API. So I feel like I'm writing the same code, but I also feel like I'm always rewriting the same code, and feel like there should be a best practice / pattern for getting data from a JSON REST API into a lists screens and then into related details screens and then back to related JSON REST API.
ChrisJCraft over 6 years ago
@ChrisJCraft If you want the easiest way to connect to a JSON Rest API, RestKit is still probably the best way to go.
alex over 6 years ago
Hi, yeah I agree RestKit is what I want in the example. I'd love to have an example that called a Rest API, made a list, then had a related detail screen, that was either view, add, edit, update, delete using the same REST API and RestKit.
ChrisJCraft over 6 years ago
awarded to alex

Crowdsource coding tasks.

2 Solutions


Hello. I assume that you already have code for JSON REST API controller. Here is how i'm work with JSON data.

All data kept in Data controller. This is singleton. In general it is recommended to map JSON objects into your own classes and then hold them as properties inside of Data controller. But actually you can have and NSMutableDictionary properties for that purpose.

@interface DataController : NSObject
@property (strong, nonatomic) id userData, userInfo, item;// containers, they hold last results of proper functions.

+(DataController *) sharedController;//return singleton instance.

@end

@implementation DataController
static DataController* _sharedController = nil;

+(DataController*)sharedController{

if(!_sharedController){
    _sharedController = [[DataController alloc] init];
}
return _sharedController;

}// this method creates and returns singleton.

@end

Once come kind of request is finished you can save parsed JSON like this.
self.userData = [NSMutableDictionary dictionaryWithDictionary:result];
result - parsed json dictionary.

Now you can easily access singleton from everywhere. (You have to import DataContoller.h).
For example show it in a list.
item = [[[DataController sharedController].userData valueForKey:@"phones"] objectAtIndex:indexPath.row]; //indexPath inside of cellForRowFor index path.

Next you can pass data to detail view.

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"authorDetails"]) {

    [[segue destinationViewController] setValue:[DataController sharedController].userData forKey:@"userName"];
}

}// user is property of details view.

After you changed data inside details view, go back to table view, and reload it. Now it will show changed data.

If this solution was helpfull to you, but you need more details let me know.

Winning solution

Overview

Okay, so I created this example application named RESTlist because it uses a REST API to list items (I'm not that good at naming things). I am using RestKit version 0.20.0-pre3 (which is the latest as of writing).

What it does

The application is basically a master-detail application that will load a set of items (called articles) on run. The app allows you to add and delete items and will sync the changes with the server via the REST API (See "The API" below). The application currently does not have update functionality because I am still trying to figure out the best way to process UITextView changes.

Device Support

The application should support all devices on iOS 6.0+. I didn't bother messing with optimizing for the new 4-inch devices (in case you wanted to deploy to another device).

Source Code

You can download the source code here.

The API

The "API" I use is a custom PHP system which uses a textfile to store the data (because it's simpler to use a text file). You can download the code here to look around, but it is not safe for real-world deployment. The syntax for requests looks like this:

GET  /read
    returns all articles as a collection
POST /post
    creates a new article
POST /delete
    deletes an existing article
Hi Alex, very nice. I did have a couple more questions for you. Can you email me? http://scr.im/chrisjcraft Also source code was missing the data.json file.
ChrisJCraft over 6 years ago
View Timeline