views:

346

answers:

1

Hi Friends,

I have developed a small iPhone application by using singleton that I use to navigate through the views. Here is a sample method from my singleton class.

+ (void) loadMenuController:(NSMutableArray *)menuItems{
     MenuViewController *menuViewControler = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
     [menuViewControler setMenuItems:menuItems];
     RootViewController *root = (
     P2MAppDelegate *appDelegate = (P2MAppDelegate*) [[UIApplication sharedApplication] delegate];
     UINavigationController *navController = [appDelegate navigationController];
     [navController pushViewController:menuViewControler animated:YES]; 
     [menuViewControler release];
}

Now my requirement has changed to require a tab view controller . I could change my application delegate to a tabview controller but I still need to navigate inside each tab. I am unable get a clue how to navigate from my singleton class.

Please guide me. Please let me know if my query is not clear.

Thanks in advance.

Regards, Malleswar

A: 

You shouldn't be using a singleton to manage the interface and even if you did, you wouldn't put the UI logic in a class method. You need to rethink your design from scratch.

The normal pattern is to hold the navigation controller or the tabbar controller as an attribute of the application delegate. The app delegate itself should not be a subclass of any controller but just a NSObject subclass that implements the application delegate protocol.

Look at the Apple supplied template projects in Xcode to see the quick and dirty way to structure apps built around navigation and/or tabs.

Singletons should only be used when you have to ensure that one and only one instance of class is alive at one time. You don't need to make your own singleton to manage the UI. The application delegate is attached to the application object which is itself a singleton. This means the app delegate provides all the restriction on class for the UI you might need. You don't need another singleton in addition to that.

Overuse of singletons is dangerous and can cause your design to get trapped in a dead end resulting in a massive rewrite. Think carefully before employing them.

TechZen
Thanks for the reply. Its really nice description. Actually to access the web service I used event based service access. Like didReceiveResponse, didReceiveData, connectionDidFinishLoading. When it reaches connectiondidfinishloading , I access single ton class and display the next controller. Can you suggest me any otherway of accessing webservice so that I can avoid accessing the controllers in singleton class. Thanks in advance.
Yes, use a delegate that will be notified of the response being complete.
Paul Lynch
But I dont want to wait till connectionDidFinishLoading. Its event based. I want just simply call a method for the request and that method should return the response.