UINavigationController not working

2019-06-14 02:25发布

问题:

I was using a UITabBar for my application but (for my own reasons) I had to remove it. Now none of my UINavigationControllers work. Where that manifests itself most is when I call: [self.navigationController pushViewController:myViewController animated:YES];

That code now does nothing because I have no UINavigationController and I don't know how to make one that will work.

回答1:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{             
    //Initialize window components
    UINavigationController *myNavController     = [[UINavigationController alloc] init];
    [self setNavController: myNavController];
    [myNavController release];

    FirstViewController *cViewController    = [[FirstViewController alloc] init];
    [self setMyFirstView: cViewController];
    [cViewController release];

    [self.window addSubview: [self navController].view];

    [[self navController] pushViewController: myFirstView animated:YES];

    [self.window makeKeyAndVisible];
    return YES;
}

This should do the trick.

By the way, you should declare the following for that in the delegate.h:

@property (nonatomic, retain) UINavigationController *navController;
@property (nonatomic, retain) IBOutlet CatchengoViewController *viewController;
@property (nonatomic, retain) UIViewController *myFirstView;


回答2:

Create a navigation controller and add it to the view as a subview or initialise it with your main view controller. It can be done from IB too. Google for navigation based app sample code for more info.