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

时间:2014-06-17 21:01:28

标签: ios objective-c uiwebview uibutton

我需要帮助。我有两个按钮,我需要每个按钮在我的UIWebView中打开一个不同的网页(例如:button1打开网站apple.com,按钮2打开google.com)。我找不到任何可以帮助我的教程。请求帮助(我使用故事板)

有我的代码,但有些问题,因为我看到只有黑屏(点击按钮后)

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

1 个答案:

答案 0 :(得分:0)

使用情节提要时,如果您将项目文件上传到某处,将会有所帮助。

但根据经验,您需要将webView委托设置为您的班级 WebViewController

你可以在viewDidLoad中添加它:

self.webView.delegate = self;

那么你必须实现 shouldStartLoadWithRequest 委托调用,这是一个好的开始。

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

让我知道这是否可以解决问题。

修改 检查项目后,将 WebViewController - &gt; 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];
}

你的_viewWeb没有按照我的怀疑进行初始化,请参阅我对如何初始化它以及如何将其添加到 WebViewController

的评论

<强> EDIT2: 添加了Web视图的约束以全屏显示,我建议您阅读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];
}