如何使用导航控制器以编程方式设置故事板中的根视图控制器

时间:2014-12-31 13:34:07

标签: ios objective-c iphone xcode

我有我的登录屏幕,当我在主页时,我能够跳过该屏幕并转到主页我能够在我的主页上获得我的导航控制器我有我的嵌入式导航控制器登录页面通过push segue连接到主页,一切都很好,但如果我尝试在主页中嵌入导航控制器,导航栏不会进入主页,那么它也不会来......我的代码是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
     NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];

     if (![defaults boolForKey:@"registered"])

     {   NSLog(@"no user register ");
     UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
     ViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:@"LoginPage"];
     self.window.rootViewController = secondViewController;
     // then set your root view controller

     }

     else

     {   NSLog(@"user is daam sure registered");


     UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] ;
         ;
     ABDB_CAMERAHOME *mainviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:@"CameraHome"];

              self.window.rootViewController = mainviewcontroller;
     }

    return YES;
}

是我们缺少关于程序中导航控制器的约束请建议

3 个答案:

答案 0 :(得分:0)

在故事板中的最新Xcode 6.1中,它们是导航控制器栏可见性的设置。您可以勾选“显示导航栏”进行显示。

答案 1 :(得分:0)

在你的appDelegate.h中添加以下属性并在.m

中合成

@property(strong,nonatomic)UINavigationController *navigationController;

@synthesize navigationController;

现在修改app delegate,如下所示

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:@"registered"])
    {   NSLog(@"no user register ");
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        ViewController *secondViewController = [storyBoard instantiateViewControllerWithIdentifier:@"LoginPage"];
        self.navigationController=[[UINavigationController alloc]initWithRootViewController:secondViewController];
        self.window.rootViewController = self.navigationController;
        // then set your root view controller
    }

    else

    {   NSLog(@"user is daam sure registered");
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] ;
        ;
        ABDB_CAMERAHOME *mainviewcontroller = [storyBoard instantiateViewControllerWithIdentifier:@"CameraHome"];
        self.navigationController=[[UINavigationController alloc]initWithRootViewController:mainviewcontroller];
        self.window.rootViewController = self.navigationController;
    }

    return YES;
}

设置完所有后,检查你的故事板是否在那里提到了正确的xib标识符名称?此外,如果您在那里使用了任何导航控制器,也请删除它。现在从构建设置中清除项目并重新运行。

答案 2 :(得分:0)

用户使用导航设置rootViewController的方法:

[(UINavigationController*)self.window.rootViewController pushViewController:yourViewController animated:YES];