Four lessons in, and at this point you should have a decent enough understanding of the Xcode interface, projects and classes to get started *gasp* writing some code. I do still need to delve deeper into syntax, including datatypes, access modifiers (or alternative conventions) and some pre-iOS 5.0 nuances, among other things, but for now let’s just have some fun.

*Gasp* Declaring Our First Method

We’re going to be working out of the DetailViewController class for this setup, so make sure to have its header file (DetailViewController.h) active in the navigator. If you recall from the third lesson, this is where the class’s interface resides, and as such, acts as the reference guide for other classes to know how to interact with it. Right now there are two properties declared and no methods. Let’s change that by adding a method of our own. We want a way to set the background color of the detail view externally, so we’re going to create a method named “applyColor” that accepts a parameter of type UIColor (which is an object). Take a moment and see for yourself if you know how to write this method. No really, stop reading. Ok, ready? This is what you should have:

#import <UIKit/UIKit.h>
 
@interface DetailViewController : UIViewController
 
@property (strong, nonatomic) id detailItem;
 
@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
 
- (void)applyColor:(UIColor *)color; // <-- our new method
 
@end

Remember, the minus sign denotes an instance method, the (void) return since we’re obviously not returning anything, and the asterisk in the parameter type declaration, since UIColor is an object. It’s also important to make sure the method is defined between the @interface and @end tags, or else it won’t end up inside the interface declaration. That’s it! Now for the implementation.

*Gasp* Implementing Our First Method

The implementation logic belongs in the implementation file, so now it’s time to switch to DetailViewController.m. (Pro Tip: The keyboard shortcut to quickly switch to a file’s counterpart is ^⌘↑ or ^⌘↓). Just copy/paste the line you added to the header file directly below the “Managing the detail item” pragma mark (between lines 20 and 21) and append code to make it look like the example below:

#pragma mark - Managing the detail item
- (void)applyColor:(UIColor *)color {
     self.view.backgroundColor = color;
}
 
- (void)setDetailItem:(id)newDetailItem
{
...

So here we’re just taking the color property passed into the method and assigning it to the backgroundColor property of the class’s view. Pretty standard. Now to actually use this method.

*Gasp* Calling Our First Method

The DetailViewController gets instantiated inside MasterViewController, so that is where we’ll want to call our method from. Since we’re not changing the public exposure of the master, we don’t need to modify the header file — just go straight to MasterViewController.m. If you scroll down to the bottom, you should see this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (!self.detailViewController) {
         self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
     }
     [self.navigationController pushViewController:self.detailViewController animated:YES];
}

Here is where the detail gets instantiated and passed into the navigation controller, so this is where we’ll want to call our new method. This is what the code should be modified to look like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (!self.detailViewController) {
         self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
         UIColor *color = [UIColor colorWithRed:48/255.0 green:116/255.0 blue:165/255.0 alpha:1.0];
         [self.detailViewController applyColor:color];
     }
     [self.navigationController pushViewController:self.detailViewController animated:YES];
}

A small segue, many of the provided Objective-C classes offer static methods for conveniently instantiating objects, which is what we’re using to create our color object. (Unfortunately, there’s no convenience method for setting a color with hex, though there are a few other options aside from RGBA if you feel like exploring.) The formal instantiation approach is to use [[UIColor alloc] init], like the way the detail view controller is instantiated. Square-bracket syntax allows for nesting method calls, and initialization must occur after allocation before an object can be used, hence why the two calls are always combined. As for passing the color in — no surprises there.

*Ahem* Testing The Application

Time to see the update in action. Hit Run and press “Detail” in the simulator app. You should see the detail view in a lovely shade of blue.

Ah, 48/255.0, 116/255.0, 165/255.0. Magnificant.

Tagged with:
 

3 Responses to Flex to Objective-C: 5-Updating DetailViewController

  1. [...] Flex to Objective-C: 5-Updating DetailViewController [...]

  2. Matt says:

    If you get to this section and the ‘tableView’ methods are all commented out and the logic doesn’t look correct. Create a new project and double check that you don’t have ‘storyboard’ checked. Using the new Storyboard stuff auto-generates some different methods.

    Great posts! Keep them coming!

  3. Jim says:

    The color changes as described when running on iPhone 5.1 simulator but not for iPad 5.1 simulator.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Set your Twitter account name in your settings to use the TwitterBar Section.