Calling Code In A Different Controller With Objective-C

I’m fairly new to the Objective-C and it might not come as a surprise that I’m only learning it because it’s the default iPhone programming language. What geek owns an iPhone and didn’t at some point want to create its own application? Well for me it’s not just for the personal pleasure, it also my job, but this post isn’t about my reasons, I just wanted to clarify that I’m not an expert and because of that the information I’ll provide might not be completely accurate or even the recommended solution.

So, I had this application based on the “Utility Application” XCode’s template which provides you with a FlipView. The template default is to create 2 views and a flip view with an header and a button that toggles the current view. The code is nice and useful but, if like me, you wanted to customize either view using the Interface Builder, you might hit the road-block of how to call the toggleView function declared on RootViewController.m…

Nicely enough there isn’t much posted around the web about this particular problem (again… since I’m new to Objective C I might just be looking on the wrong places or with the wrong keywords). But somewhere I found the tip that solved my problem, with style.

Before the actual solution I used, let me point out that as in any object oriented language you’ve always the option to pass a reference to the RootViewController to your “child” views. Nice… but messy and quite less flexible as the following solution.

So, Objective-C and Cocoa has this concept of Notifications which are just messages you can send to the whole application and anything (or any number of things) can be listening. Very much like C# delegates but without the need to know where the delegate declaration is. The main advantage of this approach is that you can use it all along your project, defining several “key” messages your application need to handle and create a modular application pretty well isolated from this “key” functions. So, how does one use this?

  1. On RootViewController.m viewDidLoad function you place this code:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleView) name:@”ToggleViewNotify” object: nil];
  2. Wherever yo want to call toggleView (for example, on the event of a button):
    [[NSNotificationCenter defaultCenter] postNotificationName:@”ToggleViewNotify” object:nil];

Pretty easy and effective. One note tough, to create and maintain this notification names, you might want to create a class with NSString vars and place the correct names in there. By including the .h file on each file using the notifications and using the vars from the object you’ll be making sure your compiler checks for typos as well as making it easier to find the right name for the notification you wanted to send.

Lastly, the NSNotificationCenter allows you to pass 1 object only, if your target method needs more arguments I suggest you pass an array and create and intermediate method to split the array and call the final method.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , ,

Leave a Reply

For spam filtering purposes, please copy the number 3476 to the field below: