Saturday 17 July 2010

Quick and easy UINavtigationController

I want to add a little section to the app I'm writing to allow the user to edit various settings for the app. Now these settings are a bit more complex than can be provided by the iPhones built in settings app, and I want the users to modify the settings in app as opposed to have to go to the separate Settings app.

So I decided to roll my own.

I have never used a UINavigationController control before, but it makes sense to use one for this area of my app as the settings involve navigating through various nested views - exactly what UINavigationController is designed for.

I pull out my iPhone book and read up on UINavigationController and run through a few tutorials on the net, it all seems easy enough!

Then I try to integrate this into my app.

And the problems begin.. All the tutorials assume the Navigation Controller will be managing all your views and are created in the Root View controller xib. I don't want this, I want it to be created by my settings view.

After several hours of messing about, I got bored and decided to just create one in code. Amazingly this was so easy!

Create the View Controller that you want to set off. Create a navigation controller with your View Controller, set a few options and then call presentModalViewController.

MainSettingsController *controller = [[MainSettingsController alloc] initWithNibName:@"MainSettingsController" bundle:nil];
controller.options = self.options;

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: controller];
nav.navigationItem.prompt = @"Settings";
controller.title = @"settings";
nav.toolbarHidden = false;
[self presentModalViewController:nav animated:YES];



You're done. No faff at all!

Wednesday 7 July 2010

Reflection

Like all emotional languages, Objective C has the ability to reflect upon itself.

This means given a class you can tell stuff about the class such as its name, what properties it has and so on.

I want to do this as I have some classes in my app and I want the user to be able to customise the options for them. Now I could of course create an XML file that contains the options that the user could change which I could then read into an NSDictionary or something and get my classes to work on them. But this is repetition.

I would much rather the options class could just read the properties of my class and display these to the user.

Reflection is slightly complicated as it needs some fairly low level C code. This is the Objective way!

 In Objective C each object is actually a struct that contains a pointer to an object called Class. This Class object contains all the information about the properties and methods that that object has.

We can access this class by calling

[ourObject class]; 


To access the reflection functions you need to

#import <objc\runtime.h>


Then the following code will get the properties and loop through them, grabbing the name from each property:


        unsigned int outCount;
        objc_property_t *classProperties = class_copyPropertyList([self class], &amp;outCount);       
        for (int i = 0; i &lt; outCount; i++)
        {
            objc_property_t property = classProperties[i];
            const char *propName = property_getName(property);

            

            // Do something with the name here ...!


        }