闪屏不会显示

时间:2010-10-26 16:11:50

标签: xcode

编辑1:将代码更改为:

delegate.h:

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ViewController *viewController;
    UIImageView *splashView;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ViewController *viewController;
@property (nonatomic, retain) IBOutlet UIImageView *splashView;

- (void)removeSplash;

@end

delegate.m:

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

@implementation AppDelegate

@synthesize window;
@synthesize viewController;
@synthesize splashView;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Splash" ofType:@"png"]];
    [window addSubview:splashView];
    [window bringSubviewToFront:splashView];
    [window makeKeyAndVisible];

    [self performSelector:@selector(removeSplash) withObject:nil afterDelay:5.0];

    [window addSubview:viewController.view];

    return YES;
}

- (void)removeSplash {
    [splashView removeFromSuperview];
    [splashView release];
}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

@end

编辑2:

当我使用时:

splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Splash" ofType:@"png"]];
if (splashView.image == nil) {
    NSLog(@"splashView is nil");
}

它记录“splashView是零”

我的Viewcontroller是空的,仅用于调试目的。

1 个答案:

答案 0 :(得分:2)

正如您可能已经知道的那样,不鼓励使用闪屏。由于您的图片是Default.png,它是否已经自动在应用启动时显示?

在任何情况下,sleep()调用都可能阻止UI。删除sleep()并将其后面的语句(removeFromSuperview等)移动到app delegate中的另一个方法。使用performSelector:withObject:afterDelay:调用此方法。将performSelector调用放在当前有睡眠调用的位置。

此外,您应该使用didFinishLaunchingWithOptions方法而不是旧的applicationDidFinishLaunching方法。