根据来自警报的segue在webview中加载不同的URL

时间:2014-07-22 14:07:38

标签: ios objective-c xcode uiwebview

在我的应用中,有不同的按钮可以触发不同的警报视图。每个人都可以选择点击"更多信息"它指向同一个UIWebView。我想在webview中加载不同的URL,具体取决于将其指向webview的警报。 我尝试创建不同的segue标识符并附加了该代码。但是webview是空白/没有加载网址。有没有办法使用不同的segue标识符来完成这项工作?或者有更好的方法来实现这一点,我可以使用相同的segue进行所有警报,但仍然在webview中加载不同的URL?如何告诉webview要加载哪一个?

HomeViewController.m

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){
    if (alertView.tag==1) {

        [self moreInfo];
    }
}


}

-(void) moreInfo{

[self performSegueWithIdentifier:@"morebainfo" sender:nil];

}
-(void)ftmoreinfo{
[self performSegueWithIdentifier:@"moreftinfo" sender:nil];

}


- (IBAction)bainfo:(id)sender {

UIAlertView *baAlert =[[UIAlertView alloc] initWithTitle:@"Bacillus anthracis"
                                                         message:@"This is the message."
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:@"More Information", nil];

baAlert.tag=1;
[baAlert show];
}
- (IBAction)ftinfo:(id)sender {

UIAlertView *ftAlert =[[UIAlertView alloc] initWithTitle:@"Francisella tularensis"
                                                 message:@"This is the message."
                                                delegate:self
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:@"More Information", nil];




[ftAlert show];
}

WebViewController.m

@interface MoreInfoViewController ()

@end

@implementation MoreInfoViewController
@synthesize MoreInfoWeb;
@synthesize loadUrl;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString: @"morebainfo"]){
    MoreInfoViewController *webview = [segue destinationViewController];
    webview.loadUrl = @"http://example.com/";
}



}



- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

NSURL *urlToLoad = [NSURL URLWithString:self.loadUrl];
[self.MoreInfoWeb loadRequest:[NSURLRequest requestWithURL:urlToLoad]];


}

1 个答案:

答案 0 :(得分:0)

以下是我最终修复它的方法:

我将准备segue方法移动到初始视图控制器:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSLog(@"prepareForSegue: %@", segue.identifier);


if ([segue.identifier isEqualToString: @"morebainfo"]){
    MoreInfoViewController *webview = [segue destinationViewController];
    webview.loadUrl = @"http://www.cdc.gov/anthrax/";

}

然后我给每个警报它自己的标签:

- (IBAction)bainfo:(id)sender {

UIAlertView *baAlert =[[UIAlertView alloc] initWithTitle:@"Title"
                                                        delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:@"More Information", nil];

baAlert.tag=1;
[baAlert show];
}

每个标记都称为不同的方法:

 -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1){
    if (alertView.tag==1) {

        [self moreInfo];
    }
}

这些方法执行了segues:

-(void) moreInfo{

[self performSegueWithIdentifier:@"morebainfo" sender:nil];

}

Fianlly,在webviewcontroller的viewdidLoad中,我补充道:

NSURL *WebUrl = [NSURL URLWithString:self.loadUrl];
NSURLRequest *WebRequest = [NSURLRequest requestWithURL:WebUrl];
[MoreInfoWeb loadRequest:WebRequest];

这很有效!帮助的主要是将segue方法的准备工作转移到初始视图控制器而不是webview控制器