iPhone应用程序:启动画面后避免白屏。让启动画面延迟,在UIWebview加载后隐藏它?启动画面没有正确隐藏

时间:2012-06-13 20:59:57

标签: ios objective-c iphone uiwebview

我们的目标对于iPhone应用程序来说很简单:显示启动页面,然后在UIWebview准备好显示其页面时隐藏它。

我们需要默认的启动画面才能继续,直到UIWebview准备好显示。否则,会短暂显示白屏。

不幸的是,在我们让它停顿后,启动画面无法隐藏。默认的初始屏幕仍然可见,隐藏在下面的UIWebview。

我们知道这可能违反Apple指南。

对于原型而言,这比任何事情都要多,我们想了解我们做错了什么。有线索吗?

我们正在使用Xcode 4.2。

AppDelegate.m:

//
//  AppDelegate.m
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize imgv;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    imgv = [[UIImageView alloc] init];
    [imgv setImage:[UIImage imageNamed:@"Default.png"]];
    [imgv setFrame:CGRectMake(0, 0, 320, 480)];
    [self.window addSubview:imgv];

    return YES;
}

@end

ViewController.m:

//
//  ViewController.m
// 
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@implementation ViewController

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIImageView *imageView = appDelegate.imgv;
    [imageView removeFromSuperview];
    [imageView setHidden:YES];
    imageView = nil;

    [self.view addSubview:webView];
    [self.view bringSubviewToFront:webView];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Done loading UIWebView");
}

@end

Curret ViewController.m生成错误:

//
//  ViewController.m
//  Stroll
//
//  Created by Macintosh User on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@implementation ViewController

@synthesize splash;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

    webView.delegate = self;

    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    for (id subview in webView.subviews)
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
            ((UIScrollView *)subview).bounces = NO;

    /*
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIImageView *imageView = appDelegate.imgv;
    [imageView removeFromSuperview];
    [imageView setHidden:YES];
    imageView = nil; */

    [self.view addSubview:webView];
    [self.view bringSubviewToFront:webView];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Done loading UIWebView");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        self.view.userInteractionEnabled = NO;

        splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        splash.image = [UIImage imageNamed:@"Default.png"];
        [self.view addSubview:splash];
    });
}

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

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [splash removeFromSuperView];
    });
}

@end

3 个答案:

答案 0 :(得分:3)

这是实现它的一种方法,首先摆脱AppDelegate中的所有代码。在根视图控制器中添加一个名为“splash”的类UIImageView的实例变量。

现在在rootViewController.m中:

-(void) viewWillAppear:(BOOL) animated {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        self.view.userInteractionEnabled = NO;

        splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

        splash.image = [UIImage imageNamed:@"Default.png"];
        [self.view addSubview:splash];
    });
    }

现在在你的webView加载完成回调方法/块

static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{

          [splash removeFromSuperView];
        });

所以dispatch_once确保该方法在应用程序的生命周期中只运行一次。

编辑:

要获得回电:

在viewController.h中的

- > viewC:UIViewController< UIWebViewDelegate>

在viewController.m中

-(void) viewDidLoad{

    CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

    webView.delegate = self;

    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"http://www.cnn.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}

然后

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

    static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{

          [splash removeFromSuperView];
        });
}

答案 1 :(得分:0)

默认情况下,Default.png用作启动画面,然后自动删除。但是您创建了另一个实例并将其自己放置到视图中。删除这部分代码

imgv = [[UIImageView alloc] init];
[imgv setImage:[UIImage imageNamed:@"Default.png"]];
[imgv setFrame:CGRectMake(0, 0, 320, 480)];
[self.window addSubview:imgv];

来自application:didFinishLaunchingWithOptions:方法

答案 2 :(得分:0)

[First ViewController.m]

启动画面没有消失的原因是因为在将启动画面添加到屏幕窗口之前调用了ViewController中的ViewDidLoad。

具体来说,ViewDidLoad方法在调用splash之前调用[self.window makeKeyAndVisible]。

[Second ViewController.m]

由于以下两个原因,您可能不希望在此处添加启动画面:1)在ViewWillAppear中,可能尚未创建视图; 2)ViewWillAppear:可以多次调用你可能不想要的。

[可行的方式]

可行的方法(我正在使用的)是你的第一次和第二次试验的组合。我的意思是

  1. 将启动画面添加到AppDelegate中的窗口;
  2. 执行一些后台任务来获取数据(例如,或加载webview);
  3. 任务完成后,发布通知。注册到TaskDoneNotification的启动屏幕将处理必要的清理并从超级视图中删除自己。
  4. 完成