Cover by Alasdair Allan

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Connecting the Controller to the Model

Now that we’ve built the model, we have to go back to the RootController class and build the bridge between the view controller and the model. To do this we need to make only one change in the RootController interface declaration (RootController.h). Add a pointer to an NSMutableArray that you’ll then populate inside the viewDidLoad: method:

@interface RootController : UIViewController
  <UITableViewDataSource, UITableViewDelegate> {
    UITableView *tableView;
    NSMutableArray *cities;
}

Changes to the implementation (RootController.m) are only slightly more extensive. You need to #import both the City.h and CityGuideDelegate.h interface files, as you’ll be using both of these classes inside the updated implementation:

#import "RootController.h"
#import "CityGuideDelegate.h"
#import "City.h"

As I mentioned earlier, you must implement the viewDidLoad: method. This UIViewController method is called after the controller’s view is loaded into memory, and is the method we’ll normally use to set up things that the view needs. You’ll find that the Xcode template included a stub for viewDidLoad (not far from the #pragma mark-labeled instance methods), but it’s commented out (wrapped inside a comment, so it doesn’t compile). Replace it with the following (be sure to remove the /* and */ so that it’s no longer commented out):

- (void)viewDidLoad { CityGuideDelegate *delegate = (CityGuideDelegate *)[[UIApplication sharedApplication] delegate]; cities = delegate.cities; ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required