【问题标题】:Only black screen in my UIWebView我的 UIWebView 中只有黑屏
【发布时间】:2014-05-29 15:51:01
【问题描述】:

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

这是 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;
}

【问题讨论】:

  • 在viewDidLoad中,创建requestObject后,尝试NSLog(@"%@, %@", theURL, requestObject);并发布结果。
  • 结果是:Tabulkaprvku[2983:60b] apple.com, { URL: apple.com }
  • 好的,很好。但我没有看到你在任何地方实例化 webview。在[webView loadRequest: requestObject] 之前尝试NSLog(@"%@", webView);。我想它可能是零。
  • 结果是:2014-05-29 18:36:36.353 Tabulkaprvku[3154:60b] (null)

标签: ios objective-c xcode webview uiwebview


【解决方案1】:

您需要在某个时候实例化 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: 方法中,在该方法中进行其他实例化。

【讨论】:

  • 我尝试将线放入 - (void)viewDidLoad,现在我得到了结果: Tabulkaprvku[3325:60b] >
  • @Alien - 好的,太好了。但听起来你的问题没有消失?您是否将插座连接到 XIB/Storyboard 中的 Web 视图?
  • Y 我已将 webView 与 Outlet 中的 Web 视图连接起来(我使用 Storyboard):(
  • @Alien - 那么问题不在于您发布的代码。 (至少,不再)。祝你好运解决这个问题。
  • 好吧,我会试试的。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2021-04-19
相关资源
最近更新 更多