Creating Navigation Bar programmatically with storyboard

筅森魡賤 提交于 2019-12-13 04:49:06

问题


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

    window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    UINavigationController *navVC = [UINavigationController new];
    ViewController *VC1 = [ViewController new];

    [window makeKeyAndVisible];

    [navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];

    [window setRootViewController:navVC];

    return window;
}

When calling this method, I get my Nav Bar but I don't see what i have in my ViewController view in the storyboard. I can't figure out what i'm doing wrong though, i've tried adding subview and stuff but nothing works out. Does anyone know what i am doing wrong?


回答1:


you need to write this code instead of what you have written

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

self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UINavigationController *navVC = [UINavigationController new];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"your storyboard name" bundle:nil];

ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];


[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];

 [self. window makeKeyAndVisible];

return YES;

}




回答2:


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

{

self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

UINavigationController *navVC = [UINavigationController new];
navVC.navigationBar.backgroundColor = [UIColor blackColor];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];




 FirstViewController *VC2 = [mStoryboard instantiateViewControllerWithIdentifier:@"First"];
SecondViewController *VC3 = [mStoryboard instantiateViewControllerWithIdentifier:@"Second"];

[VC2.view addSubview:VC3.view];
 [navVC setViewControllers:@[VC1,VC2]animated:NO];

[self. window setRootViewController:navVC];

 [self. window makeKeyAndVisible];

return YES;

}



来源:https://stackoverflow.com/questions/20337425/creating-navigation-bar-programmatically-with-storyboard

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