【问题标题】:How do I change view controller when returning from image picker?从图像选择器返回时如何更改视图控制器?
【发布时间】:2014-03-06 23:03:26
【问题描述】:

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

  1. 从控制器 MAViewControllerMenu 我可以选择现有图片
  2. 选择后,我通过关闭选择器视图返回 MAViewControllerMenu
  3. 在返回 MAViewControllerMenu 后,我想切换到另一个控制器 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];
}

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

谢谢

【问题讨论】:

    标签: ios objective-c controller


    【解决方案1】:

    利用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];
     }];
    }
    

    演示

    【讨论】:

    • 我试过了。我不会工作的。但是,我注意到我收到一条消息“警告:尝试在 上显示 ,其视图不在窗口层次结构中!”
    • 将你的代码从 viewDidLoad 移动到 viewDidAppear: ,我猜 viewdidload 没有被调用
    • 好一个。我确实将其移至 viewDidAppear。现在它在 [self presentViewController:imageControllerView animated:YES completion:nil];//THAT DOESNT WORK 的 NSexception 上崩溃了
    • @Alon_T 你还需要帮助吗?告诉我
    【解决方案2】:

    试试这个:

    而不是弹出 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 - 知道这是一种 hack,但它确实有效!如果您调用显示没有动画的 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];
         }
    

    【讨论】:

    • 它导致它与 NSException 崩溃
    • 你是对的,我很抱歉,这仅适用于 UINavigationController,我自己检查了一下..
    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2012-10-02
    相关资源
    最近更新 更多