Struggling with Navigation Controller basics
I've been pulling my hair out trying to get a basic NavigationController working that will allow me to switch back and forth between views easily. I feel like I'm making progress, but I'm clearly missing something critical. I now have my template app pushing views, but only by adding the initWithNibName pointing to the target NIB. Attempting to add any functionality to these secondary views causes the app to crash with a SIGABRT error. I can't imagine this is right.. If I simply have a plain NIB, the switch works fine. The only thing I've added to the secondViewcontroller is a label and a button to populate the label with some garbage text. Yet the instant I hit the switch button to shift push this view i get the SIGABRT. I'd like to be able to put functionality within the different view controllers. I feel like i'm so close, but tis is so aggravating. Can anyone point out where i've gone wrong?
#import "ma开发者_C百科inViewController.h"
@implementation mainViewController
-(void)switchView {
UIViewController *secondViewController = [[UIViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
secondViewController.title = @"My First View";
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
}
-(void)switchViewTwo {
UIViewController *thirdViewController = [[UIViewController alloc] initWithNibName:@"thirdViewController" bundle:nil];
thirdViewController.title = @"My second View";
thirdViewController.view.backgroundColor = [UIColor redColor];
[self.navigationController pushViewController:thirdViewController animated:YES];
[thirdViewController release];
}
Instead of
UIViewController *secondViewController = [[UIViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
Put this:
MySecondViewController *secondViewController = [[MySecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
My MySecondViewController
should be the name of your UIViewController
, also check that the XIB's name is really called secondViewController. Finally:
- Go to your XIB
- Select the File's Owner file.
- Select the 3rd tab in the inspector.
- Check the name of the class. It should be
MySecondViewController
instead ofUIViewController
.
精彩评论