如何从View Controller

时间:2016-02-11 18:19:04

标签: ios objective-c xcode uinavigationcontroller nib

我正在制作一个他们不是故事板的项目。

我对xib不太了解。

我遇到了从Xib推送ViewControllers的问题。

在故事板上,我曾经使用过:

 [self.navigationController pushViewController......];

但在这里我该怎么做:

我正在尝试使用此代码:

 HomeScreenVC *homeScreen = [[HomeScreenVC alloc]initWithNibName:@"HomeScreenVC" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:homeScreen];
[navController pushViewController:homeScreen animated:YES];

但崩溃和有效的代码是:

 UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:self.signInVC];
[controller presentViewController:myNavigation animated:YES completion:nil];

我无法找到正确的方法。请协助。

2 个答案:

答案 0 :(得分:0)

首先尝试使用导航控制器设置viewcontroller

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//The first view controller
        self.firstController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
//The navigation controller
        UINavigationController *navController =  [[UINavigationController alloc]initWithRootViewController:self.firstController];
        [self.window addSubview:navController.view];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
}

让我们说ViewController是你的类推送到你的新viewcontroller,尝试以下:

@implementation ViewController

- (void)goToHomeScreen{
     HomeScreenVC *homeScreen = [[HomeScreenVC alloc]initWithNibName:@"HomeScreenVC" bundle:nil];
     [self.navigationController pushViewController:homeScreen animated:YES];
}

答案 1 :(得分:0)

您的代码块1崩溃,原因是:

HomeScreenVC *homeScreen = [[HomeScreenVC alloc]initWithNibName:@"HomeScreenVC" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:homeScreen];
[navController pushViewController:homeScreen animated:YES];

您已将homeScreen添加到导航控制器,并再次尝试推送同一个控制器。

您的根视图控制器(welcomeVC)没有导航控制器,因此它无法推送任何视图控制器(它只能呈现,因此阻止的第二个代码为您工作)。您必须从欢迎VC呈现导航控制器,其具有主屏幕作为根视图控制器。

或者

您需要在应用程序委托中将导航控制器添加为根视图控制器(在导航堆栈中具有WelcomeVC),然后通过简单地调用以下命令将其推送到welcomeVC中的homeVC:

[self.navigationController pushview....

AppDelegate:在此处将导航控制器设置为根视图控制器:

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

       WelcomeVC *mainViewController = [[WelcomeVC alloc]initWithNibName:@"WelcomeVC" bundle:nil]; 
       UINavigationController *navController =  [[UINavigationController alloc]initWithRootViewController: mainViewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
}

然后推送到你希望的控制器:

 HomeScreenVC *homeScreen = [[HomeScreenVC alloc]initWithNibName:@"HomeScreenVC" bundle:nil];
 [self.navigationController pushViewController:homeScreen animated:YES];
相关问题