Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6387013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:05:57+00:00 2026-05-25T03:05:57+00:00

I am a newbie in iOS application development, but I am trying to learn

  • 0

I am a newbie in iOS application development, but I am trying to learn how to deal with Cocoa in the best way.

I got stuck trying to understand how to keep and reference the model objects properly.

  1. many say to write an app delegate property to hold the model and then reference it through the convenience methods for the app delegate singleton.
  2. others say to “inject” in the view controller only the part of model which it needs (or its subviews needs), but I don’t understand how to do this. Via a property? Via an initWithModel: method (and in this case, how can I say to IB to use that method?)
  3. others again say that the model should be a singleton
  4. and again, others say to use global variables (!)

Could you please give me some hint (and code samples)? I would like to learn the things in the proper manner, considering that soon I will be moving towards Core Data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:05:58+00:00Added an answer on May 25, 2026 at 3:05 am

    Abstract: I read carefully the topic Where to place the "Core Data Stack" in a Cocoa/Cocoa Touch application suggested by Brad Larson and I wrote a possible solution on how to deal with a model and different view controllers. The solution doesn’t use Core Data, but I believe that the same design may be applied to Core Data apps.

    Scenario: let’s consider a simple application which stores information about products, such as name, description and price/unit. Once launched, the application shows a list of products (with a UITableView); when the user taps on a product name, the application presents product details in another view, updating the navigation bar with the product name.

    Architecture The model is pretty simple here: an array of Product objects, each one with a name, a description and a price property.

    The application has got three main views, mostly created by the Navigation template of Xcode: a UINavigationView (managed by the UINavigationController, instantiated in the app delegate), the default UITableView (managed by RootViewController and which is the first view shown by the UINavigationController) and a DetailView (managed by the DetailViewController class we have to write).

    Let’s see what’s the big plan from the model point of view:

    1. The model is instantiated/loaded by the Application delegate as a NSMutableArray of Product objects;
    2. The pointer to the model is now passed to the first view controller of our hierarchy, UITableViewController, through a property. Actually, one could argue that the first controller in the hierarchy is the UINavigationController, so we should pass the reference to it and from it to the UITableViewController but… Apple says that UINavigationController shouldn’t be subclassed, so we cannot add any property/method. And actually it makes sense, because the responsibility of an UINavigationController is always just the visualization management, not the model manipulation.
    3. When the user select a UITableCell, the UITableViewController creates a new DetailViewController (with the associated DetailView), passes the single selected product as a property and pushes the DetailView on the top of the UINavigation stack.

    Here some code snippets:

    Creation of the model:

    // SimpleModelAppDelegate.m    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // products is a protected ivar
        products = [[NSMutableArray alloc] init];
    
        Product *p1 = [[Product alloc] initWithName:@"Gold" andDescription:@"Expensive metal" andUnitPrice:100];
        Product *p2 = [[Product alloc] initWithName:@"Wood" andDescription:@"Inexpensive building material" andUnitPrice:10];
    
        [products addObject:p1];
        [products addObject:p2];
    
        [p1 release];
        [p2 release];
    
        // Passing the model reference to the first shown controller 
        RootViewController *a = (RootViewController*)[self.navigationController.viewControllers objectAtIndex:0];
        a.products = products;
    
        // Add the navigation controller's view to the window and display
        self.window.rootViewController = self.navigationController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)dealloc
    {
        // The app delegate is the owner of the model so it has to release it.
        [products release];
        [_window release];
        [_navigationController release];
    
        [super dealloc];
    }
    

    The RootViewController can receive the model reference, since it has a NSMutableArray property:

    // RootViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UITableViewController
    
    @property (nonatomic, retain) NSMutableArray *products;
    
    @end
    

    When the user taps on a product name, the RootViewController instantiates a new DetailViewController and passes the reference to the single product to it using a property again.

    // RootViewController.m
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    
        // Passing the model reference...
        detailViewController.product = [products objectAtIndex:indexPath.row];
    
        [self.navigationController pushViewController:detailViewController animated:YES];
    
        [detailViewController release];
    }
    

    And, at the end, the DetailViewController shows the model information setting its outlets in the viewDidLoad method.

    // DetailViewController.m
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.navigationItem.title = product.name;
        self.descriptionLabel.text = product.description;
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f eur", product.unitPrice];
    }
    

    You can download the full project here: http://dl.dropbox.com/u/1232650/linked/stackoverflow/SimpleModel.zip

    I will really appreciate any comment to my solution, I am eager to learn 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am newbie to IOS application development. I have to parse an XML document
I am an iOS development newbie. I want to go to another page (CountryViewController)
I'm an iOS newbie, but was wondering if it's possible to use a built-in
Standard newbie question. I've created a data model for an iOS application. I am
I'm newbie in iOS development and I have a question. I have a some
I am newbie to iphone programming and trying to learn iphone programming.I need experts
I am newbie working on iOS application for iPad. I have successfully implemented a
I'm a newbie iOS developer. I wrote a small application that save an NSMutableArray
I am a total newbie on iOS development. After numerous tries; with lots of
I'm newbie at iOS development and this is my first question on SO. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.