IPhone SDK:如何在Alert视图中触摸按钮时打开Webview?

时间:2010-10-14 03:10:35

标签: iphone sdk uiwebview uialertview

我将WebView.h导入到我的项目中,它只有一个简单的IBOutlet UIWebView *myWebView ;

如何在警报视图中按下按钮时打开WebView?

这是我如何定义警报视图按钮响应

- (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
        }
    }
}

注意:我可以弹出我的Webview,但是如何为我想调用的webview初始化URL地址?

2 个答案:

答案 0 :(得分:1)

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];
        }
    }
}

答案 1 :(得分:0)

web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

NSString *urlAddress = [[videolistArray objectAtIndex:indexPath.row] valueForKey:@"video"] ;


NSURL *url = [NSURL URLWithString:urlAddress];


NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
[self.view addSubview:web];