OGeek|极客世界-中国程序员成长平台

标题: ios - 如何将另一个 UIViewController 添加到现有的 UINavigationController [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:45
标题: ios - 如何将另一个 UIViewController 添加到现有的 UINavigationController

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

在用户从照片库中选择图像后,我向第二个 View Controller 展示此代码:

-(void)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)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];
}



Best Answer-推荐答案


而不是

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

使用

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

这样VC2也在同一个Navigation栈中

您的最终代码如下所示:

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

关于ios - 如何将另一个 UIViewController 添加到现有的 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921983/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4