4

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

現在の 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
4

3 に答える 3

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 は、メソッドがアプリケーションの存続期間中に 1 回だけ実行されるようにします。

編集:

コールバックを取得するには:

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];
        });
}
于 2012-06-13T22:19:48.370 に答える
0

[最初のViewController.m]

スプラッシュ スクリーンが消えない理由は、スプラッシュ スクリーンがスクリーン ウィンドウに追加される前に ViewController の ViewDidLoad が呼び出されるためです。

具体的には、スプラッシュが呼び出される直前の [self.window makeKeyAndVisible] 付近で ViewDidLoad メソッドが呼び出されました。

[2番目のViewController.m]

おそらく、ここにスプラッシュ スクリーンを追加したくない理由は 2 つあります。1) ViewWillAppear では、ビューがまだ作成されていない可能性があります。2) ViewWillAppear: 何度も呼び出される可能性がありますが、実際には必要ないかもしれません。

【実行可能な方法】

実行可能なアプローチ (私が使用しているもの) は、1 回目と 2 回目の試行の組み合わせです。つまり

  1. AppDelegate のウィンドウにスプラッシュ スクリーンを追加します。
  2. バックグラウンド タスクを実行してデータを取得します (たとえば、Web ビューの読み込み)。
  3. タスクが完了したら、通知を投稿します。TaskDoneNotification に登録されたスプラッシュ スクリーンは、必要なクリーンアップを処理し、自身をスーパービューから削除します。

終わり

于 2012-07-18T02:31:42.743 に答える
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:方法から

于 2012-06-13T21:53:18.507 に答える