Search code examples
iphonesdkuiwebviewuialertview

IPhone SDK : how to open a Webview when touch a button in Alert view?


I import WebView.h to my project,and it only has a simple IBOutlet UIWebView *myWebView ;

How to Open a WebView when I press the button in the alert view ?

here is how I define alert view button response

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView tag]==0) {
        if (buttonIndex == 1)
        {   
           //Open WebView with URL string1 
        }
        if (buttonIndex == 2) {
           //Open WebView with URL string2
        }
    }
}

note:I can pop up my Webview , but How to init a URL address to the webview I wanna call ?


Solution

  • UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    [self.view addSubview:webView];
    
     -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if ([alertView tag]==0) {
    
            NSURLRequest *urlRequest;
            NSURL *urlforWebView;
    
            if (buttonIndex == 1)
            {   
               urlforWebView = [NSURL URLWithString:@"http://www.google.com"];
               urlRequest = [NSURLRequest requestWithURL:urlforWebView];
               [webView loadRequest:urlRequest];
            }
            if (buttonIndex == 2) 
            {
               urlforWebView = [NSURL URLWithString:@"http://www.yahoo.com"];
               urlRequest = [NSURLRequest requestWithURL:urlforWebView];
               [webView loadRequest:urlRequest];
            }
        }
    }