简体   繁体   English

单击按钮后,在UIWebView中打开网页

[英]Open web page in UIWebView after click on the button

I need help. 我需要帮助。 I have two button, and I need each button to open a different web page in my UIWebView (for example: button1 open website apple.com, button2 open google.com). 我有两个按钮,每个按钮都需要在UIWebView中打开不同的网页(例如:button1打开网站apple.com,button2打开google.com)。 I can not find any tutorial that would help me. 我找不到任何对我有帮助的教程。 Thx for help (I use Storyboard) . 寻求帮助(我使用情节提要)

There is my code, but something is wrong, because I see only a black screen (after click on the button) 有我的代码,但出了点问题,因为我只看到黑屏(单击按钮后)

TabulkaViewController.m TabulkaViewController.m

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton.frame = CGRectMake(10, 240, 300, 35);
    [myButton setTitle:@"Buttonone" forState:UIControlStateNormal];
    [myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: myButton];
}
-(void) myButtonClick:(NSString *)myString
{   
    NSURL *url = [NSURL URLWithString:@"http://www.apple.com/"];
    WebViewController *viewWeb = [[WebViewController alloc] initWithURL:url andTitle:@"Apple"];
    [self presentViewController:viewWeb animated:YES completion:nil];

}

WebViewController.h WebViewController.h

#import <UIKit/UIKit.h>

@interface WebViewController : UIViewController <UIWebViewDelegate>
{
NSURL *theURL;
NSString *theTitle;
IBOutlet UINavigationItem *webTitle;
}
- (id)initWithURL:(NSURL *)url;
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
@property (strong, nonatomic) IBOutlet UIWebView *viewWeb;
@end

WebViewController.m WebViewController.m

#import "WebViewController.h"
@implementation WebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    webTitle.title = theTitle;
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
    [_viewWeb loadRequest:requestObject];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (id)initWithURL:(NSURL *)url andTitle:(NSString *)string {
    if( self = [super init] ) {
        theURL = url;
        theTitle = string;
    }
    return self;
}
-(id)initWithURL:(NSURL *)url {
    return [self initWithURL:url andTitle:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _viewWeb.delegate = nil;
    [_viewWeb stopLoading];
}
@end

When working with storyboard, it would help if you upload your project file somewhere. 使用情节提要时,如果将项目文件上载到某处,将很有帮助。

But from experience you need to set your webView delegate to your class WebViewController 但是根据经验,您需要将webView委托设置为类WebViewController

you can do it by adding this in your viewDidLoad: 您可以通过在viewDidLoad中添加以下代码来做到这一点:

self.webView.delegate = self;

then you must implement the shouldStartLoadWithRequest delegate call, this is a good start. 那么您必须实现shouldStartLoadWithRequest委托调用,这是一个好的开始。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{ 
    return YES;
}

let me know if this fixes it. 让我知道是否可以解决。

EDIT: after checking your project, change WebViewController -> viewDidLoad to this: 编辑:检查项目后,将WebViewController- > viewDidLoad更改为此:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // init webview
    _viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];

    _viewWeb.delegate = self;

    // add it to this controller's view
    [self.view addSubview:_viewWeb];

    webTitle.title = theTitle;
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];

    [self.viewWeb loadRequest:requestObject];
}

your _viewWeb wasn't initialized as I suspected, See my comments on how to initialize it and how to add it to your WebViewController 您的_viewWeb并未按照我的怀疑进行初始化,请参阅有关如何初始化它以及如何将其添加到WebViewController的评论

EDIT2: Added constraints in for the web view to take full screen, I recommend you read-up about auto layout in apple docs EDIT2:添加了使Web视图全屏显示的约束,建议您阅读有关Apple文档中自动布局的内容

- (void)viewDidLoad
{
    [super viewDidLoad];
    // init webview
    _viewWeb = [[UIWebView alloc] initWithFrame:self.view.frame];

    _viewWeb.delegate = self;

    // add it to this controller's view
    [self.view addSubview:_viewWeb];


    // add constraints
    NSDictionary* dict = @{@"viewWeb":_viewWeb};
    _viewWeb.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
    [self.view addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[viewWeb]-0-|" options:0 metrics:nil views:dict];
    [self.view addConstraints:constraints];

    webTitle.title = theTitle;
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];

    [self.viewWeb loadRequest:requestObject];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM