-1

I have two buttons in my VC and currently they both are connected to their respective pdf files. There is another button, Push, that goes to the rootviewcontroller. In the RVC I have a UIWebView. How can I make it so that if I push button A, a.pdf is displayed and b.pdf for button B?

I guess, how do I make the rvc listen for this event correctly?

Snippet for UIWebView inside my RVC.m

pdfViewer=[[UIWebView alloc]initWithFrame:CGRectMake(420, 190, 340, 445)];
NSString *path = [[NSBundle mainBundle] pathForResource:@"nameofpdf" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[pdfViewer setAlpha: .8];
[pdfViewer loadRequest:request];
[self.view addSubview:pdfViewer];

And button Push in my VC

    -(IBAction)pushButtonPressed{

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:@"Root"];
        [UIApplication sharedApplication].delegate.window.RootViewController = RVC;
}

Wrap up: I first press A or B, then press Push which will bring me to the RVC which will display pdfA or pdfB.

user3325170
  • 133
  • 10

1 Answers1

1

Declare a property for your RVC for the pdf file you want it to open. In your viewController that holds the buttons have the buttonFunction pass the filename to the segue as the sender parameter in prepareForSegue. In prepareForSegue grab the dest view controller and set the property. A short snippet might be more clear

In your viewController with the Buttons:

- (void)buttonA {
    [self performSegueWithIdentifier:@"mySegue" sender:@"pdfA.pdf"];
}

- (void)buttonB {
    [self performSegueWithIdentifier:@"mySegue" sender:@"pdfB.pdf"];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"mySegue"]) {
        UIViewController *rvc = segue.destinationViewController;
        rvc.pdfToOpen = sender;
    }
}

You'll have to adjust your prepareForSegue to use the correct class with the correct property name of course, but this is generally how you should pass information from an action into a view controller via a segue.

Once in your RVC you can access that property, probably inside viewDidLoad, and tell your webView to load the correct pdf.

Brandon
  • 2,387
  • 13
  • 25
  • Thanks @brandon roth for your response, I tried this before but I changed the code a little bit (reflected in my edits above). I have another button that switches the views, and added that portion to my code. could you please help me with that? – user3325170 Jun 30 '14 at 12:57
  • This worked when I made it a segue. I'm going to open a different question because I see that what I asked above is a different method than what you posted. – user3325170 Jun 30 '14 at 13:34