在这种情况下如何实现UINavigationController?

时间:2012-09-05 12:25:56

标签: objective-c ios uiviewcontroller uinavigationcontroller

我项目的当前版本:

  

我的应用中有5个不同的UIViewControllers。我设定了我的   FirstViewController使用Initial View Controller   属性检查员。我从一个ViewController来回移动   另一个是通过使用我分配模态segue的按钮   ViewController到另一个,使用StoryBoard

我想要改变的内容:

  

我想明显保留导航按钮,删除模态segue并使用   改为UINavigationController。如果我理解这个概念   正确地说,当使用UINavigationController时,我需要进入每个   UIButton-IBAction并且在方法的最后我必须推动下一个   ViewController我想移动到我的NavigationController上(我也是   必须首先弹出当前的一个?)。但是,我无法弄清楚如何   正确地实现所有这一切。

到目前为止我做了什么:

  • 我从故事板中删除了所有模态segue并保留了导航按钮及其相应的IBActions
  • 我取消选中属性检查器中的框,该框使我的FirstViewController成为我应用的初始视图控制器
  • 我进入了我的 AppDelegate.m 并尝试在那里创建导航控制器并使我的FirstViewController成为RootViewController

MyAppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *myFirstViewController = [[FirstViewController alloc] init];
    UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];

    [myNavigationController pushViewController:myFirstViewController animated:YES];

    // Override point for customization after application launch.

    return YES;
}
  • 然后我尝试通过进入a的IBAction来测试上述是否有效 我的FirstViewController上的导航按钮并实现了 以下为了移动到我的SecondViewController时 按下按钮:

FirstViewController.m

- (IBAction)goRightButton:(UIButton *)sender
{
    // some code drawing the ButtonIsPressed UIImageView on the current View Controller

    UIViewController *mySecondViewController = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:mySecondViewController animated:YES];
}

但没有任何反应。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您没有链接您的XIB文件。请将您的导航控制器添加为

UIViewController *myFirstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:myFirstViewController];

使用以下代码从一个视图移动到另一个视图

UIViewController *mySecondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:mySecondViewController animated:YES];

答案 1 :(得分:0)

如果您使用的是故事板,则只需将导航控制器拖入其中并将其连接到您的应用代表即可。只要它是主要故事板,并且您已确定首先加载视图控制器,您就不需要在应用程序委托中加载任何视图。

为了以编程方式推送故事板中的视图,您需要执行以下操作:

 //bundle can be nil if in main bundle, which is default
 UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 MyCustomViewController *customVC = (MyCustomViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"customVC"];
//standard way
[self.navigationController pushViewController:customVC animated:YES];

//custom animation
[UIView transitionWithView:self.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
    [self.navigationController pushViewController:customVC animated:NO];
} completion:nil];

使用在storyboard编辑器中添加的标识符标识视图控制器。下面是一些帮助显示我的意思的截图。

navigation controller with arrow to indicate initial load

place where you change the identifier of the view controller, so you can call it