如何将另一个UIViewController添加到现有的UINavigationController

时间:2015-04-28 14:10:30

标签: ios objective-c uiviewcontroller uinavigationcontroller

我有一个带有两个View Controller的应用程序。第一个嵌入在导航控制器中并有一个导航栏,在第一个视图控制器中某个方法结束后,我以编程方式呈现第二个视图控制器。这样可以正常工作,但这样第二个视图控制器不是导航控制器堆栈的一部分,并且与第一个视图控制器没有任何关系。像这样,我无法利用导航控制器。关于如何解决这个问题的任何想法?

在用户从照片库中选择图像后,我使用此代码显示第二个视图控制器:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  [self dismissViewControllerAnimated:YES completion:NULL];
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewControllerID"];
  viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  [self presentViewController:viewController animated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:1)

而不是

[self presentViewController:viewController animated:YES completion:nil];

使用

[self.navigationController pushViewController:viewController animated:YES];

这样VC2也在同一个导航堆栈中

您的最终代码如下所示:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    ViewController *viewController = (ViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewControllerID"];    
    viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController pushViewController:viewController animated:YES];
}

答案 1 :(得分:0)

试试这个:

YourController *controller = [[YourController alloc] init];

[self.navigationController pushViewController:controller animated:YES];