XCode 4.2: Controlling the Detail View from the Master-Detail Template (iOS 5)

In XCode 4.2 (using the iOS 5 SDK), the Master-Detail Application template has replaced the old Split View Application template. Many of the components such as the RootViewController class and the MainWindow.xib no longer exist. The template is much simpler, but it does take some getting use to. The main problem is to control the DetailViewController from the MasterViewController. When running the template without modifications, the MasterViewController only controls itself, and has no control over the DetailViewController. This is a walkthrough of the process of gaining access to the detail side.

Getting Started with the Master-Detail Application Template

1. Start a new project then select the Master-Detail Application Template.

 

2. For this sample, set the Device Family to iPad and disable the “Use Storyboard”.

 

3. Run the app in your iPad simulator, and you’ll notice when you click the menu, it only affects itself, not changing the large detail view.

 

When looking at the code from MasterViewController.h, there is a pointer called detailViewController, this is not a reference to the large detail view in the split view. Instead, it is a reference to the new menu screen that will be pushed on to of the menu table.

Referencing the DetailViewController

At this point there is no way to change anything in the large DetailViewController. This is how you add that reference:

1. Add the following line to the MasterViewController.h file:

@property (strong, nonatomic) DetailViewController *mainDetailViewController;

 

2. Add the synthesize statement to your MasterViewController.m file:

@synthesize mainDetailViewController;

 

3. Add the reference to mainDetailViewController from AppDelegate.m:

masterViewController.mainDetailViewController = detailViewController;

 

4. Now you can start making changes to the large DetailViewController. Let’s change the wording in the label. In the didSelectRowAtIndexPath function in the MasterViewController.m, add the following line:

mainDetailViewController.detailDescriptionLabel.text = @"I did it!";

 

Now you can run the app, and you will notice that when you press the Detail cell, you will change the text in the main detail view.

About these ads

About Antonio Zugno
Web Developer - Experienced with HTML, C#, VB, JavaScript, jQuery, MS SQL, XML, WordPress, PHP, and Adobe Flash.

16 Responses to XCode 4.2: Controlling the Detail View from the Master-Detail Template (iOS 5)

  1. Wei says:

    Great help! I have been confused with the detailViewController in Master Detail App for quite a long time.

    Thanks for the good work.

  2. Zen says:

    very helpful tutorial, helped me on my universal app.

    Thanks

  3. Bek says:

    Thank you so much, this was a lifesaver. I have been spinning my wheels for hours trying to figure out what happened to the “old style” uisplitviewcontroller.

  4. sean friend says:

    I have set everything up as you have here but my coding is arranged very differently. I am using Xcode 4.2 for iOS 5. For example, I do not have any code matching the last screenshot. I have all of the commented out pieces but nothing past that.

    • Which files are you having problems with? The DetailViewController.m file, or multiple files?

      • Mark says:

        I have the same prob. the code in AppDelegate.m i not matching and it will not accept the code line “masterViewController.mainDetailViewController = detailViewController;”

      • Mark, I just went through the example again from a new project. The only think I can think of is trying to build the project before adding the line to the AppDelegate.m. Sometimes I’ll even have to quit XCode and come back in for things to work. If you are still having problems, can you tell me what kind of error message you are getting?

    • Sean, I have updated the instructions in step 4. The label change is actually being made at the end of the MasterViewController.m file.

  5. Charlie Day says:

    Thank you so much, works like a charm.

    +1 Karma for you!

  6. stevenh says:

    excellent tutorial. thank you!

    simple question:
    1) we do not want the master list to transition to detail mode at all – similar to the iPad settings app.
    2) using a master list with groupings

    can your example code be adapted to this?

  7. Dan says:

    You are a rock star!

  8. Branden says:

    Thanks a bunch! I’m new to iOS programming and I absolutely love it (flexing my nerd muscle). This tutorial is awesome. As for the few that were having problems with their code not matching. Try checking your ARC to make sure it’s enabled. I realize that this is an old thread but I wanted to chime in anyway. Have a great one everybody!

  9. James says:

    Great tutorial. None of the other tutorials including Apples explain it as nicely

  10. ade says:

    hi, don’t suppose there’s any chance of updating this for xcode 5 is there?

    I’m also stuck in Step 3 trying to add line
    masterViewController.mainDetailViewController = detailViewController;

    The lines shown in that method look totally different to what my Master-Detail project has generated.

    “masterViewController” now appears to be called “controller” and “detailViewController” is apparently an “undeclared identifier” despite line “import “CCSMasterViewController.h”

    thanks in advance

    • Ade,
      I’m sorry, but I changed jobs, doing more .NET development now. I am no longer working with mobile development and haven’t kept up with xcode updates. Good luck to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: