【发布时间】: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);并发布结果。 -
好的,很好。但我没有看到你在任何地方实例化 webview。在
[webView loadRequest: requestObject]之前尝试NSLog(@"%@", webView);。我想它可能是零。 -
结果是:2014-05-29 18:36:36.353 Tabulkaprvku[3154:60b] (null)
标签: ios objective-c xcode webview uiwebview