2010-10-26 107 views
0

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是空的,僅用於調試目的。

回答

2

正如你可能已經知道的,不鼓勵閃屏。由於您的圖像是Default.png,它是不是已經在應用程序啓動時自動顯示?

無論如何,sleep()調用可能會阻塞UI。刪除sleep()並將它後面的語句(removeFromSuperview等)移動到應用程序委託中的另一個方法。使用performSelector:withObject:afterDelay:調用此方法。將performSelector調用放在當前有睡眠呼叫的地方。

此外,您應該使用didFinishLaunchingWithOptions方法而不是舊的applicationDidFinishLaunching方法。

+0

雖然不鼓勵啓動屏幕,但如果您的應用程序需要一秒或兩秒以上的加載,我認爲確保它看起來不像可以與之交互的UI是很好的UI。您的用戶界面上的一個較小的警報閃屏看起來相當不錯。 – 2010-10-26 16:54:34

+0

當我把它放在我的資源文件夾中時,Default.png不會自動工作。我們在說話時正在嘗試其他解決方案。 – lugte098 2010-10-26 20:27:10

+0

同時將'[window addSubview:viewController.view];'移到'removeSplash'方法(使其成爲該方法的最後一行)。 – Anna 2010-10-26 20:43:47