2014-03-06 90 views
0

我试图做一些简单:从图像选择器返回时如何更改视图控制器?

  1. 从控制器MAViewControllerMenu我可以选择一个现有的图片
  2. 选择后,我在返回到MAViewControllerMenu返回MAViewControllerMenu通过驳回选择器视图
  3. 权,我想切换到另一个控制器MAViewControllerPictureDisplay,我可以在其中查看在imageView中选择的图像。

但是,它不会把我带到下一个视图。

下面是MAViewControllerMenu代码

MAViewControllerPictureDisplay* imageControllerView; 
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    //load next page 
    imageControllerView = [[self storyboard]  instantiateViewControllerWithIdentifier:@"chosenImageController"]; 
} 



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    image = [info objectForKey:UIImagePickerControllerOriginalImage];//Take image from picker 
    imageControllerView.image = image;//set it for the next controller's property 
    [self dismissViewControllerAnimated:YES completion:^{ 
    [self.presentingViewController presentModalViewController:imageControllerView 
                animated:YES]; 
}]; 
    //NEXT LINE DOESNT WORK 
    [self presentViewController:imageControllerView animated:YES completion:nil];//THAT DOESNT WORK 
} 

然而,我在我的第一个控制器中设置的按钮时,在一个点击运行该线之上通过函数如下,和一个不带我去下一个控制器:

- (IBAction)movetonext{ 
    [self presentViewController:imageControllerView animated:YES completion:nil]; 
} 

我错过了什么?为什么在点击按钮时调用它,但在没有触摸事件的情况下调用时,在选择图片后不起作用?

感谢

回答

0

试试这个:

相反POPING的UIViewController,然后呈现。推两个视图控制器,并弹出一个。

当你调用选择器:

[self presentViewController:imageControllerView animated:NO completion:nil]; 
    [self presentViewController:PickerController animated:YES completion:nil]; 

而且当您关闭:

[Picker dismissViewControllerAnimated:YES completion:NULL]; 

需要注意的是,当我调用两个UIViewController中提出,为他们赢得了(还没有到呈现的一个)被设置为“NO”的动画。

希望这有助于

编辑

上面的代码,只有UINaviationController作品,由 “PushViewController” 和 “PopViewController”。

实现这一点,我发现只有两个方法是:

1 - 如果用户NSNotificationCenter解雇你middel视图,如下所示:

Dismiss 2 modal view controllers

不幸的是,你

,它不是相同的情况下,所以我想为你使用UINaviationController 的方式,是使用UIImagePicker的子视图,即解散他的自己..

2 - 知道这是一种黑客,但它的工作原理!如果你打电话来介绍middel UIViewController,没有动画,并从该视图调用图像选择器,它将工作。并且您将不得不将选取的图像进行委托。 我加入到我的代码,为middel的UIViewController有一个Alpha设置为0的第一次,所以你不会注意到,他是韩元如何呈现:

//First UIViewController: 

@implementation ViewControllerOne 
- (IBAction)buttonPressed:(id)sender { 

    ViewControllerTwo *viewTwo = [[ViewControllerTwo alloc]init]; 
    [self presentViewController:viewTwo animated:NO completion:nil]; 
} 

//Secound UIViewController: 
     @interface ViewControllerTwo() 
     @property (nonatomic) UIImagePickerController *myPicker; 
     @end 
     @implementation ViewControllerTwo 

     -(void)viewWillAppear:(BOOL)animated 
     { 
     //View to alph 0 if this is the first time it displayis: 
     if (!self.myPicker) { 
     [self.view setAlpha:0]; 
     }else{ 
     [self.view setAlpha:1]; 
     } 
     } 
     -(void)viewDidAppear:(BOOL)animated{ 

     if (!self.myPicker) { 
     self.myPicker = [[UIImagePickerController alloc]init]; 
     self.myPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     self.myPicker.delegate = self; 
     [self presentViewController:self.myPicker animated:YES completion:nil]; 
     } 
     } 
     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info 
     { 
    [self.myPicker dismissViewControllerAnimated:YES completion:nil]; 
     } 
     - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
     { 
    [self.myPicker dismissViewControllerAnimated:YES completion:NO]; 
     } 
     - (IBAction)ButtonTwoClicked:(id)sender { 
     [self dismissViewControllerAnimated:YES completion:NO]; 
    } 
+0

它导致它崩溃与NSException –

+0

你是对的,我很抱歉,这只适用于UINavigationController,只是自己检查.. – MCMatan

3

利用completion处理程序:

-(void)imagePickerController:(UIImagePickerController *)picker 
            didFinishPickingMediaWithInfo:(NSDictionary *)info { 
[self dismissViewControllerAnimated:YES completion:^{ 
    image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    imageControllerView.image = image; 
    [self presentViewController:imageControllerView animated:YES completion:nil]; 
}]; 
} 

DEMO

present view

+0

我试过了。我不会工作。但是,我注意到我收到一条消息“警告:试图在上呈现,其视图不在窗口层次结构中!” –

+0

更新我的代码上面是我正在运行 –

+0

将您的代码从'viewDidLoad'移动到'viewDidAppear:',我猜viewdidload没有被调用 – meda

相关问题