简体   繁体   中英

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

I Have three View controllers.

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 . In second UIViewController which is inherited from image editor Viewcontroller after cropping my image, clicking "done" button.

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?

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

In 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:

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

After on your Detail View Controller , don't forget to assign it as delegate of the Controller . And to implement the ControllerDelegate protocol. To do that:

In your DetailController.h implement the protocol. You can do that on your .m as well.

#import "Controller.h"

@interface DetailController : UIViewController <ControllerDelegate>

You keep a reference of the Controller in The Detail Controller

@property (nonatomic, strong) ViewController *controller;

And assign its delegate in the viewDidLoad of the Detail Controller . 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. Here you receive the image you passed from Controller and you save it in your Detail Controller UIImage property:

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

   self.image = image;
}

You can implement storyboard segue to your detail controller and use following sample code.

- (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];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM