Still really new to iOS dev (have a background in asp.net / web), and I don’t think I’ve quite gotten my head around how everything relates to everything else. For example, I’m building an app at the moment which starts with a NavigationController. I’m passing ViewControllers in and out of that quite happily and everything is working but now I need to add a rightbutton to the navigation controller. I have done this from within one of the ViewControllers like this:
- (void)viewDidLoad
{
UIBarButtonItem *change = [[UIBarButtonItem alloc] initWithTitle:@"CHANGE" style:UIBarButtonItemStylePlain target:self action:@selector(navAlert)];
self.navigationItem.rightBarButtonItem = change;
[change release];
[super viewDidLoad];
}
- (void)navAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"nav pressed" message:@"You pressed the Change button" delegate:nil cancelButtonTitle:@"Yep" otherButtonTitles:nil];
[alert show];
[alert release];
}
but I don’t want to have to do that in each controller – should something like this go into the AppDelegate and be called from there?
Also, suppose I didn’t want to start with a NavigationController – could I use code in the AppDelegate to release ViewControllers and load new ones directly into the Window, by clicking a button which is set up in a ViewController?
Sorry if this is a bit of a stupid question, but I don’t think I’ve quite grasped how the different layers relate, what code is available to which controller, etc.
Thanks
You should not put interface related code inside the appdelegate. That’s what the ViewControllers are there for. If there’s functionality which is common to all of your viewcontrollers, I’d suggest subclassing UIViewController (and then deriving all other instances from this subclass) or writing a category on it.
The appdelegate on the other hand is a good place to trigger basic events tied to the lifespan of the app, like setting up core data contexts or saving the application state when it is about to enter the background.