Search code examples
iphoneiosios6

Can't get this segue of an image and caption to work


In this code a user gets and displays a photo (provided for now) in ImageViewController, adds a caption, and the the photo and caption are passed to the MTViewcontroller where it is made into a pdf. I have solved the PDF portion, but struggling with the segue. Appreciate any guidance. I can't find an example quite like this.

ImageViewController.h

#import <UIKit/UIKit.h>
@interface ImageViewController : UIViewController
{
    NSURL *url;
    UIImage *imageRoll;
}
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UITextField *enterCaption;
- (IBAction)Button:(id)sender;
@end

ImageViewController.m Not sure I'm treating imageRoll correctly and I can't find the right segue format for this. Once the user, submits the caption it would segue to the PDF controller. BUT, would the keyboard release given the segue?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Since iPhone simulator doesn't have photos, load and display a placeholder image
    NSString *fPath = [[NSBundle mainBundle] pathForResource:@"IMG_3997" ofType:@"jpg"];
    url = [NSURL fileURLWithPath:fPath];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    UIImage *imageRoll = [UIImage imageWithContentsOfFile:fPath];
}

// User provides a caption for the image
- (IBAction)Button:(id)sender {
    NSString *caption = enterCaption.text;
    [self performSegueWithIdentifier:@"showPDF" sender:sender];
    MTViewController *vc1 = [segue destinationViewController];
    vc1.photoCaption = caption;
    MTViewController *vc2 = [segue destinationViewController];
    vc2.photoImage = imageRoll;
}
//Dismiss Keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

MTViewController.h

#import <UIKit/UIKit.h>
#import "ReaderViewController.h"
@interface MTViewController : UIViewController <ReaderViewControllerDelegate>
@property(nonatomic, assign) NSString *photoCaption;
@property(nonatomic, assign) UIImage *photoImage;
- (IBAction)didClickMakePDF:(id)sender;
@end

MTViewController.m Have I passed and used photoCaption and photoImage correctly?

- (IBAction)didClickMakePDF {
    [self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100];

    [self beginPDFPage];

    CGRect textRect = [self addText:photoCaption
                          withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];

    CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                                       withColor:[UIColor blueColor]];

    UIImage *anImage = [UIImage imageNamed:@"photoImage"];
    CGRect imageRect = [self addImage:anImage 
                              atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];

    [self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                 withColor:[UIColor redColor]];

    [self finishPDF];

}

Solution

  • updated:

    You should use self.xxx = xxx to save it in one method and get it back in another method by using self.xxx. For example you should use

    self.imageRoll = [UIImage imageWithContentsOfFile:fPath];
    

    instead of declaring a new imageRoll in your code. And it's the same with your caption.

    The way you use segue is incorrect.

    The way you call [self performSegueWithIdentifier:@"showPDF" sender:sender]; is correct. But then you can do some customization in

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"showPDF"]) {
            MTViewController *vc1 = [segue destinationViewController];
            vc1.photoCaption = self.caption;
            vc1.photoImage = self.imageRoll;
        }
    }