简体   繁体   中英

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). 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

    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

#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

#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

you can do it by adding this in your viewDidLoad:

self.webView.delegate = self;

then you must implement the shouldStartLoadWithRequest delegate call, this is a good start.

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

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

EDIT2: Added constraints in for the web view to take full screen, I recommend you read-up about auto layout in apple docs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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