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.
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.