如何在页面加载后仅在应用程序启动后显示UIWebView?

时间:2013-04-04 21:08:27

标签: iphone objective-c xcode ios4

我正在尝试仅在页面加载后加载UIWebView。在页面准备好之前,我只希望显示启动画面。我尝试按照此页面上的第一个答案(XCode Prevent UIWebView flashing white screen after Launch Image)但它似乎不起作用......启动画面加载,UIWebView永远不会加载。

有什么建议吗?

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

IBOutlet UIWebView *webView;

}

@property (nonatomic, retain) IBOutlet UIWebView *webview;
@property(nonatomic, strong) UIImageView *loadingImageView;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController;

@synthesize webview;
@synthesize loadingImageView;

- (void)viewDidLoad
{
    [super viewDidLoad];


    //**************** Set website URL for UIWebView
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://http://warm-chamber-7399.herokuapp.com/"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];


    //**************** Add Static loading image to prevent white "flash" ****************/
    UIImage *loadingImage = [UIImage imageNamed:@"Default.png"];
    loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
    loadingImageView.animationImages = [NSArray arrayWithObjects:
                                        [UIImage imageNamed:@"Default.png"],
                                        nil];
    [self.view addSubview:loadingImageView];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    // Remove loading image from view
    [loadingImageView removeFromSuperview];

}

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

@end

3 个答案:

答案 0 :(得分:4)

试试这个,

@interface ViewController () <UIWebViewDelegate>

 {
    UIWebView * webView;
    UIImageView * imageView;
 }

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    imageview = [[UIImageView alloc]init];
    [imageview setFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
    [imageview setImage:[UIImage imageNamed:@"Red.png"]];
    [self.view addSubview:imageview];

    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
    [webView setDelegate:self];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"]]];

    // Do any additional setup after loading the view, typically from a nib.
}

// UIWebView Delegate

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [imageView removeFromSuperview];
    [self.view addSubview:webView]; 
}

@end

答案 1 :(得分:1)

您是否设置了webView的代理人?如果不设置,您的webViewDidFinishLoad:方法将永远无法启动。

webView.delegate = self;

答案 2 :(得分:0)

为什么不直接将启动图像添加到.plist文件