简体   繁体   English

使用推视控制器时如何将Uiimage从一个视图控制器传递到其他视图控制器?

[英]How to Passing Uiimage from one view controller to Other view controller when using push view controller used?

I Have three View controllers. 我有三个View控制器。

First UIViewController contain one UIImage , when I click "crop" button, go to second view controller I pass the image by push view controller, here I'am cropping my UIImage . 第一个UIViewController包含一个UIImage ,当我点击“crop”按钮时,转到第二个视图控制器我通过推视控制器传递图像,这里我正在裁剪我的UIImage In second UIViewController which is inherited from image editor Viewcontroller after cropping my image, clicking "done" button. 在裁剪图像后继承自图像编辑器Viewcontroller的第二个UIViewController中,单击“完成”按钮。

I need to pass the cropped image to another view controller where i am having share button to share the post. 我需要将裁剪的图像传递给另一个视图控制器,我在其中分享按钮来分享帖子。

Problem is when I am puishing the editted image to another view controller in which share button is present,image is not passing. 问题是当我将编辑的图像推到另一个存在共享按钮的视图控制器时,图像没有通过。

You are saving the image in the Detail Controller before you make the push? 在进行推送之前,您是将图像保存在Detail Controller中?

DetailController *detailVC=[[DetailController alloc]
                          initWithNibName:@"DetailController"
                          bundle:nil];

detailVC.image = self.image;

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

Delegation 代表团

You implement the protocol on your View Controller 您在View Controller上实现协议

In Controller.h : Controller.h

@class Controller;

@protocol ControllerDelegate <NSObject>

- (void)sendFrom:(Controller *)controller
                   image:(UIImage *)image;

@end

@interface Controller : UIViewController

@property (weak, nonatomic) NSObject <ControllerDelegate> * delegate;

@end

In Controller.m when you want send the image: Controller.m当您想要发送图像时:

[self.delegate sendFrom:self
         image: self.image];

After on your Detail View Controller , don't forget to assign it as delegate of the Controller . Detail View Controller ,不要忘记将其指定为Controller委托。 And to implement the ControllerDelegate protocol. 并实现ControllerDelegate协议。 To do that: 要做到这一点:

In your DetailController.h implement the protocol. 在您的DetailController.h实现协议。 You can do that on your .m as well. 您也可以在.m上执行此操作。

#import "Controller.h"

@interface DetailController : UIViewController <ControllerDelegate>

You keep a reference of the Controller in The Detail Controller 您将在Detail Controller保留Controller的参考

@property (nonatomic, strong) ViewController *controller;

And assign its delegate in the viewDidLoad of the Detail Controller . 并在Detail ControllerviewDidLoad中分配其delegate For example: 例如:

-(void)viewDidLoad:(BOOL)animated{

      [super viewDidLoad:animated];

      self.controller.delegate = self;
   }

Don't forget to implement the protocol method in your Detail View. 不要忘记在详细信息视图中实现protocol method Here you receive the image you passed from Controller and you save it in your Detail Controller UIImage property: 在这里,您将收到从Controller传递的图像,并将其保存在Detail Controller UIImage属性中:

- (void)sendFrom:(Controller *)controller
                       image:(UIImage *)image{

   self.image = image;
}

You can implement storyboard segue to your detail controller and use following sample code. 您可以将storyboard segue实现到您的详细控制器并使用以下示例代码。

- (void)cropButtonClick:(id)sender
{
    [self performSegueWithIdentifier: @"MySegue" sender:sender];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"MySegue"]) {
        DetailController *detailVC = segue.destinationViewController;
        [detailVC setImage:self.image];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用popvivew控制器时,如何将Uiimage从一个视图控制器传递到另一个视图控制器? - How to Passing Uiimage from one view controller to Other view controller when using popvivew controller used? 从一个视图控制器向另一个视图控制器传递数据时出错 - Error when passing data from one view controller to the other 如何从一个视图控制器导航到另一个 - How to navigate From one view controller to other 无法将数据从一个视图控制器推送到表视图控制器 - Not able to push data from one view controller to a table view controller 从呈现视图控制器推送视图控制器 - Push View Controller from Presenting View Controller 从子视图控制器推送视图控制器 - Push view controller from a Child View controller 从子视图控制器推送视图控制器 - Push View Controller From Child View Controller 如何从一个视图推一个新视图controller - How to push a new view controller from a view 如何从多个视图控制器推送到一个视图控制器对象 - How to push to one view controller object from multiple view controllers 如何从View Controller中的情节提要中选择UIImage - How to select UIImage in storyboard from View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM