启动画面将不显示
问题描述:
EDIT 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 is nil”
我的ViewController是空的,仅用于调试目的。
答
正如你可能已经知道的,不鼓励闪屏。由于您的图像是Default.png,它是不是已经在应用程序启动时自动显示?
无论如何,sleep()调用可能会阻塞UI。删除sleep()并将它后面的语句(removeFromSuperview等)移动到应用程序委托中的另一个方法。使用performSelector:withObject:afterDelay:
调用此方法。将performSelector调用放在当前有睡眠呼叫的地方。
此外,您应该使用didFinishLaunchingWithOptions方法而不是旧的applicationDidFinishLaunching方法。
虽然不鼓励启动屏幕,但如果您的应用程序需要一秒或两秒以上的加载,我认为确保它看起来不像可以与之交互的UI是很好的UI。您的用户界面上的一个较小的警报闪屏看起来相当不错。 – 2010-10-26 16:54:34
当我把它放在我的资源文件夹中时,Default.png不会自动工作。我们在说话时正在尝试其他解决方案。 – lugte098 2010-10-26 20:27:10
同时将'[window addSubview:viewController.view];'移到'removeSplash'方法(使其成为该方法的最后一行)。 – Anna 2010-10-26 20:43:47