2

I have a problem with image transferring that I don't understand. I want to display an UIImage of an UIImageView on another controller if the user executes a "Long Press Gesture". The other controller will be shown but the image I transfer won't be displayed.

The button is connected with a third controller over a modal storyboard segue. There the user is able to capture the image. On "ok" the controller switches back to the first one and the captured image will be displayed within an UIImageView. No problems until there. To achieve the long press gesture guides to an other controller I dropped the gesture to the storyboard and connected the button to it. If the user executes the gesture following function will be executed

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if([[segue identifier] isEqualToString:@"toPreview"]){
        PreviewViewController *pvc = (PreviewViewController *)[segue destinationViewController];
        [pvc setImage:signatureView.image];
    }
}

The signatureView is an UIImageView that displays the user captured image (in this case a signature). the "setImage:(UIImage*)" function is one of many possibilities I tried.

Here is the code of the controller that should display the image

@interface PreviewViewController : UIViewController

@property(nonatomic, retain) IBOutlet UIImage *previewImage;
@property (weak, nonatomic) IBOutlet UIImageView *previewImageView;

- (void) setImage:(UIImage *)image;

@end

@implementation PreviewViewController

@synthesize previewImage;
@synthesize previewImageView;

- (void) setImage:(UIImage *)image {
    previewImage = image;
    previewImageView.image = previewImage;
    [previewImageView setNeedsDisplay];
}

...

As I already said I tried many possibilities but nothing displayed the passed UIImage within the previewImageView. I tried to write the image of the previewImageView directly from the first controller, I passed the complete UIImageView of the first controller, ...

I don't understand whats my fault. Can anybody help me? I'm using XCode 4.2 and iOS 5 SDK

Ben

ben
  • 41
  • 5
  • i got the solution. see the following link: http://stackoverflow.com/questions/7864371/ios5-how-to-pass-prepareforsegue-an-object – superstar_jy Feb 23 '12 at 00:40

2 Answers2

0

I think passing data between view controllers in iOS development is called a protocol or a @protocol. I hate to be one of the guys that says 'reference that' but thats what i'd do. cheers

user3720631
  • 53
  • 1
  • 10
0

You can not call [pvc setImage:signatureView.image]; because the signatureView does not exist when prepareForSegue: is called.

You could pass the name of the image and set it in viewWillAppear.

- (void)viewWillAppear {
    [super viewWillAppear];
    [[self signatureView] setImage: [UIImage imageNamed: imageName]];
}
dasdom
  • 13,975
  • 2
  • 47
  • 58
  • The image isn't a file and I would prefer not saving it. Within the `prepareForSegue:` I react on the normal click too and work with the controller that captures the image. In this way the image transferring works. `if([[segue identifier] isEqualToString:@"toSignature"]){ SignatureViewController *svc = (SignatureViewController *)[segue destinationViewController]; svc.sourceImageView = signatureView; }` – ben Nov 22 '11 at 09:48
  • Yes that was one of the possibilities I tried. This is why I am confused. It works in this case but not in the other... – ben Nov 22 '11 at 10:38
  • That case works because the signatureView is already loaded. BTW, you don't need to write the image to a file to make it work. – Firoze Lafeer Nov 23 '11 at 09:09