xcode 如何关闭以编程方式创建的 UIWebView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6693383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:29:10  来源:igfitidea点击:

How to close UIWebView created programmatically?

xcodeuiwebviewuibutton

提问by DeZigny

I have the following code to create UIWebView programmatically and create UIButton on top of it to close it. The creation is OK, but the problem I can't refer back to the created UIWebView to close it from the button!

我有以下代码以编程方式创建 UIWebView 并在其上创建 UIButton 以关闭它。创建没问题,但是问题我不能参考创建的UIWebView从按钮关闭它!

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 210, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];

- (IBAction)close:(id)sender {
????
}

Thanks for your help in advance :)

提前感谢您的帮助:)

回答by Vijay-Apple-Dev.blogspot.com

In ViewController.m

在 ViewController.m 中

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

// tag will be used to get this webview later
webView.tag=55;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(close:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 210, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];





- (IBAction)close:(id)sender {

    [[self.view viewWithTag:55] removeFromSuperview];


}

回答by highlycaffeinated

Keep your webView as an ivar of your UIViewController.

将您的 webView 作为 UIViewController 的 ivar。

Right now you have it as a local variable to the method so you can't refer to it outside that method. Instead, declare it in your interface section of your .h file and you'll be able to access it from all your class methods.

现在您将它作为方法的局部变量,因此您不能在该方法之外引用它。相反,在 .h 文件的接口部分声明它,您将能够从所有类方法访问它。

And don't forget to release it in your dealloc method!

并且不要忘记在您的 dealloc 方法中释放它!

回答by Deepak Danduprolu

The web view is the button's superview so you should be able to get it like this,

Web 视图是按钮的超级视图,因此您应该能够像这样获得它,

UIWebView * webView = [(UIButton *)sender superview];

Now you can do removeFromSuperviewor something else to make it disappear.

现在你可以做removeFromSuperview或其他的事情让它消失。

Edit

编辑

- (IBAction)close:(id)sender {
    UIWebView * webView = [(UIButton *)sender superview];
    [webView removeFromSuperview];
}

回答by raj kumar

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    NSLog(@"%@",request.URL.absoluteString);
    NSString *absoluteUrl = [[request URL] absoluteString];
    if ([absoluteUrl isEqualToString:@"https://google.com/login?"]) {
        NSLog(@"move to another view or close the web view");
        [portfolioWebView removeFromSuperview];

        return NO;
    }

    return YES;
}