我的UIWebView中只有黑屏

时间:2014-05-29 15:51:02

标签: ios objective-c xcode webview uiwebview

我有两个视图控制器。在第一个我有一个按钮,当你点击它时,你需要在第二个视图控制器中显示一个网页。当我点击按钮时,我只看到一个黑屏。

这是TabulkaViewController.m代码:

#import "TabulkaViewController.h"
#import "WebViewController.h"

@interface TabulkaViewController ()

@end

@implementation TabulkaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _hbutton.layer.borderWidth = 1;
    _hbutton.layer.borderColor = [[UIColor grayColor] CGColor];        
}
- (IBAction)hbutton:(id)sender

{
   NSURL *url = [NSURL URLWithString:@"http://apple.com"];
   WebViewController *webViewController = [[WebViewController alloc] initWithURL:url andTitle:@"Apple"];
    [self presentViewController:webViewController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

这是WebViewController.h的代码:

    #import <UIKit/UIKit.h>

    @interface WebViewController : UIViewController <UIWebViewDelegate>
    {
        NSURL *theURL;
        NSString *theTitle;
        IBOutlet UIWebView *webView;
        IBOutlet UINavigationItem *webTitle;

    }

    - (id)initWithURL:(NSURL *)url;
    - (id)initWithURL:(NSURL *)url andTitle:(NSString *)string;
    - (IBAction) done:(id)sender;

    @end

这是WebViewController.m的代码:

#import "WebViewController.h"

@interface WebViewController ()

@end

@implementation WebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    webTitle.title = theTitle;
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
    [webView loadRequest:requestObject];}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (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];
}

- (IBAction) done:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    webView.delegate = nil;
    [webView stopLoading];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

1 个答案:

答案 0 :(得分:0)

您需要在某个时刻实例化webView。我建议你将viewDidLoad更改为:

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView = [[UIWebView alloc] init];
    webTitle.title = theTitle;
    NSURLRequest *requestObject = [NSURLRequest requestWithURL:theURL];
    [webView loadRequest:requestObject];
}

由于您的webView在方法开头是null,因此需要先创建它,然后才能加载任何请求。只是在头文件中声明它是不够的,这就是为什么你需要webView = [[UIWebView alloc] init];

编辑:
当然,您也可以将该行放入您的ìnitWithURL:andTitle:方法,其中进行其他实例化。