Objective c 使用NSNotification检测UIButton按钮更改UIWebView

Objective c 使用NSNotification检测UIButton按钮更改UIWebView,objective-c,uiwebview,segue,nsnotificationcenter,nsnotification,Objective C,Uiwebview,Segue,Nsnotificationcenter,Nsnotification,我有两个按钮在我的VC和目前他们都连接到各自的pdf文件。还有另一个按钮Push,它指向rootviewcontroller。在RVC中,我有一个UIWebView。我怎样才能使按下按钮A时显示A.pdf,按下按钮b时显示b.pdf 我想,如何使rvc正确地侦听此事件 my RVC.m中UIWebView的代码段 pdfViewer=[[UIWebView alloc]initWithFrame:CGRectMake(420, 190, 340, 445)]; NSString *path =

我有两个按钮在我的VC和目前他们都连接到各自的pdf文件。还有另一个按钮Push,它指向rootviewcontroller。在RVC中,我有一个UIWebView。我怎样才能使按下按钮A时显示A.pdf,按下按钮b时显示b.pdf

我想,如何使rvc正确地侦听此事件

my RVC.m中UIWebView的代码段

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];
在我的VC里按下按钮

    -(IBAction)pushButtonPressed{

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        RootViewController *RVC = [storyboard instantiateViewControllerWithIdentifier:@"Root"];
        [UIApplication sharedApplication].delegate.window.RootViewController = RVC;
}
总结:
我先按A或B,然后按Push,这将使我进入RVC,RVC将显示pdfA或pdfB

为要打开的pdf文件声明RVC的属性。在保存按钮的viewController中,使用Button功能将文件名作为prepareForSegue中的发送方参数传递给segue。在prepareForSegue中,获取dest视图控制器并设置属性。一个简短的片段可能更清楚

在viewController中,使用以下按钮:

- (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;
    }
}
当然,您必须调整prepareForSegue以使用具有正确属性名称的正确类,但这通常是您应该通过segue将信息从操作传递到视图控制器的方式


一旦进入RVC,您就可以访问该属性,可能在viewDidLoad中,并告诉您的webView加载正确的pdf。

感谢@brandon roth的回复,我以前尝试过,但我对代码做了一点更改(反映在上面的编辑中)。我有另一个切换视图的按钮,并将该部分添加到我的代码中。你能帮我一下吗?当我把它做成一段时,它就起作用了。我将开始一个不同的问题,因为我看到我上面问的是一个不同于你发布的方法。