how to add uinavigation controller in a view based application

妖精的绣舞 提交于 2019-12-31 03:05:41

问题


I wanted to add a navigation controller to a view based application . how can we do this both programmatically and using xib file..


回答1:


If you need to incorporate a navigation controller in your uiviewcontroller you need to initialize it as it follows

UIViewController *yourViewController = ...

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];

[self presentModalViewController:navController animated:YES];

//you need to release the controller
[navController release];

If you are in the UIApplicationDelegate method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

You can't do a presentModalViewController:navController animated... then you need to add the navController.view to the window

    UIViewController *yourViewController = ...

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
    [self.window addSubview:navController.view];
    //don't do a release of navController because is not retained by addSubview



回答2:


UINavigationController *navcontroller = [[UINavigationController alloc] initWithRootViewController:viewController];//here viewController is to which you want to make the navigation

[self.view addSubView:navController.view];



回答3:


You can just drag a "Navigation Bar" from your objects in the bottom right corner of Interface Builder. This basically does what Sachin says in his answer but you still have to programmatically create the functionality of the navigation controller. I.e pushing new views to the stack and poping them off.

In my opinion it's easiest to do it entierly in the code.




回答4:


If you want to have a navigation controller as the root view for your main window. Then you can do so by using the following code.

@interface yourAppDelegate_iPad : NSObject <UIApplicationDelegate> {
    UINavigationController *navigationController;
}

@property (nonatomic, retain) UINavigationController *navigationController;

@end

@implementation yourAppDelegate
@synthesize navigationController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    navigationController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

You can do this by using the xib as follows

  1. Open the MainWindow.xib
  2. Drag and drop a UINavigationController to it.
  3. Create and connect the outlets.
  4. Open attributes for the navigation controller and set the root view.


来源:https://stackoverflow.com/questions/7398151/how-to-add-uinavigation-controller-in-a-view-based-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!