Splash Screen before TabBarController
I am a beginner of iPhone developer and I am working on my first apps. Actually, I already created a Tab Bar application well but I would like to add the Splash Screen when run the apps.
I found a exactly question at Loading a Welcome Screen(Splash Screen) before TabBarController
But when I try to put in my code, the splash screen doesn't load and just keep showing my tabbarcontroller.
I created a SplashViewController.h, SplashViewController.m and SplashView.xib and following is my code,
#import "SplashViewController.h"
...
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
SplashViewController *controller = [[SplashViewController alloc] initWithNibName:@"Spl开发者_开发问答ashView" bundle:nil];
[self.tabBarController presentModalViewController:controller animated:YES];
[controller release];
// Add the tab bar controller's view to the window and display.
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
The apps run without error but just cannot load the splash screen, any comment is highly appreciated. Thanks!
My guess is that the tab bar controller is ignoring your call to presentModalViewController:animated:
because it isn't on screen yet. Try moving the call to after the tab bar view has been added as a subview to the window. It may have to happen even after the call to makeKeyAndVisible
.
If your requirement is to show a view until the user taps it, then MetaLik's suggestion would work. Alternatively, you could add the splash controller's view directly to the app window.
[self.window addSubview:MySplashController.view];
In either case, you'll need to create a button or some subclass of UIResponder to respond to the user's tap, and when you get that, either dismissModalViewController or [self.view removeFromSuperview], depending on how you instantiated it.
I would suggest just adding the view of the splash screen view controller to your window and making it main. No need to use the tab bar controller to present it modally. then in the splash screen just have a button that takes up the whole screen and whenever its pressed remove and release the view and do your normal window setup (configure the tab bar and etc).
Edit: some code to show what I mean,
in your app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SplashViewController *controller = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
[self.window addSubview:controller.view];
[self.window makeKeyAndVisible];
[controller release];
}
In your splash View Controller:
-(IBAction) didPressButtonToDismiss:(id)sender {
//create a reference to the singleton class for easier typing
MyAppDelegate *delegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
[delegate.window addSubview:delegate.tabBarController.view];
[delegate.window bringSubviewToFront:delegate.tabBarController.view];
[self.view removeFromSuperview];
}
One thing to note: I am assuming you initalize and set up your tab bar controller in a nib (as it seems from your original post).
Excellent comment from Mike about how to setup a splash screen, in order to make a dirt simple splash screen create a static image called Default.png and presto magico it works. It should display for about 5 seconds max or until your app loads. I used width x height dimensions of 320x480 and it is basically exactly what I needed.
Thank you Mike.!!
Also, here is a useful link that goes along with creating a splash screen and icons etc... Custom Icon and Image Creation Guidelines
I did this and it works fine.
AppDelegate.h:
@interface AppDelegate_Pad : NSObject
<UIApplicationDelegate, SplashViewControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
SplashViewController *svc = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil];
svc.delegate = self;
[self.tabBarController presentModalViewController:svc animated:NO];
return YES;
}
-(void)splashViewControllerDidFinish:(SplashViewController *)splashViewController {
[self.tabBarController dismissModalViewControllerAnimated:NO];
}
SplashViewController.h:
@protocol SplashViewControllerDelegate;
@interface SplashViewController : UIViewController {
id<SplashViewControllerDelegate> delegate;
}
@property (nonatomic, assign) id <SplashViewControllerDelegate> delegate;
@end
@protocol SplashViewControllerDelegate
-(void)splashViewControllerDidFinish:(SplashViewController *)splashViewController;
@end
SplashViewController.m:
// Call the below line where you want to remove splash view
[self.delegate splashViewControllerDidFinish:self];
精彩评论