从图像选择器返回时如何更改视图控制器?

时间:2014-03-06 23:03:27

标签: ios objective-c controller

我正在尝试做一些简单的事情:

  1. 从控制器MAViewControllerMenu我可以选择现有的图片
  2. 选择后,我通过解除选择器视图
  3. 返回MAViewControllerMenu
  4. 在返回MAViewControllerMenu后,我想切换到另一个控制器MAViewControllerPictureDisplay,在那里我可以看到在imageView中选择的图像。
  5. 然而,它并没有把我带到下一个观点。

    这是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];
    }
    

    我缺少什么?在选择图片后,为什么在点击按钮时调用它但在没有触摸事件时调用时它不起作用?

    由于

2 个答案:

答案 0 :(得分: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];
 }];
}

<强>样本

present view

答案 1 :(得分:0)

试试这个:

而不是弹出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];
     }