使用故事板以编程方式创建导航栏

时间:2013-12-02 20:41:56

标签: uinavigationcontroller storyboard

- (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;
}

当调用这个方法时,我得到了我的导航栏,但是我没有在故事板中的ViewController视图中看到我的内容。我无法弄清楚我做错了什么,我已经尝试添加子视图和东西,但没有任何效果。有谁知道我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要编写此代码而不是您编写的代码

- (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;

}

答案 1 :(得分:0)

- (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;

}