Tuesday, April 21, 2009

Navigation Controller Fun

Add 2 (plus 2 headers automatically added) to your Classes group.  Nav1ViewController and Nav2ViewController, both using the template of UIViewController subclass.

Drag a Navigation Controller over from the toolbox and drop it onto the Tab Bar Controller.  Take a Tab Bar Item and do the same thing. Run it.  I think it "just works" and shows a new view with a navigation controller on the third tab.  

Add 2 new Nibs, from View Nib, called NavView1 and NavView2.  Set their File's Owner's class appropriately.  Associate the View Outlet with the view.  Run it.

Now, add a button to the NavView1.xib.  Go to your Nav1ViewController.h file.  Put in this line: 
-(IBAction)LoadNextPane:(id)sender; after the end of he Interface block, before the @end block.

Put this code in the .m file:
// Do something
UIViewController *targetViewController;
targetViewController = [[SecondViewController alloc] initWithNibName:@"NavView2" bundle:nil];
[[self navigationController] pushViewController:targetViewController animated:YES];


At the top of the .m file, you must include an import to the file that tells the compiler about what SecondViewController really is.

#import "SecondViewController.h".

Now we have an action (I think of it as an event, but it probably isn't).  Go back to the NavView1.xib file.

Associate your button's "Touch Up Inside" to the File's Owner, and choose the "LoadNextPane" option.  It's the only one that should appear.  Run it.  Kaboom.  Not sure why right now.

The problem was that I had not, in MainWindow.xib set the Nav1 View Controller (Navigation Item) 's class to Nav1ViewController.  Once I did that, the 2nd nav view scrolled on screen with the button push.

No comments:

Post a Comment