我是iOS开发新手。我有一个启动画面,想用初始视图替换它,我的意思是图像和两个按钮。如何设置初始视图?我在iOS 4上使用Xcode 4.这是我的代码......
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
backgroundView.backgroundColor = [UIColor lightGrayColor];
[window addSubview:backgroundView];
[backgroundView release];
// Override point for customization after application launch.
DataController *appController = [DataController sharedObject];
tabController = [[UITabBarController alloc] init];
HomeViewController *hemView = [[HomeViewController alloc] init];
[hemView setTitle:@"Hem"];
hemView.tabBarItem.image = [UIImage imageNamed:@"icon_home.png"];
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:hemView];
[homeNavigationController.navigationBar setTintColor:[UIColor colorWithRed:(0.96) green:(0.96) blue:(0.96) alpha:0.0]];
[hemView release];
SearchViewController *sdkView = [[SearchViewController alloc] init];
[sdkView setTitle:@"Search"];
sdkView.tabBarItem.image = [UIImage imageNamed:@"icon_search.png"];
UINavigationController *sdkNavigationController = [[UINavigationController alloc] initWithRootViewController:sdkView];
[sdkNavigationController.navigationBar setTintColor:[UIColor colorWithRed:(0.96) green:(0.96) blue:(0.96) alpha:0.0]];
[sdkView release];
NSArray * arrayOfControllers = [[NSArray alloc] initWithObjects:homeNavigationController, sdkNavigationController, nil];
tabController.viewControllers = arrayOfControllers;
SplashScreenViewController *controller = [[SplashScreenViewController alloc] init];
[tabController presentModalViewController:controller animated:YES];
[controller release];
[self.window makeKeyAndVisible];
[window addSubview:tabController.view];
[homeNavigationController release];
[sdkNavigationController release];
[appController release];
return YES;
}
但是这段代码没有显示启动画面,它直接带我到HomeViewController,请帮忙。
答案 0 :(得分:0)
(警告,基于ARC的代码......别忘了发布) 我这样做了:
首先,我将SplashScreenViewController
*controller
和tabController
作为appDelegate
的属性。
然后我更改didFinishLaunchingWithOption
以仅显示SplashScreenViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];
backgroundView.backgroundColor = [UIColor lightGrayColor];
[window addSubview:backgroundView];
[backgroundView release];
// Override point for customization after application launch.
DataController *appController = [DataController sharedObject];
[self controller] = [[SplashScreenViewController alloc] init];
[[self controller]setAppDelegate:self];
self.window.rootViewController = self.controller;
[self.window makeKeyAndVisible];
return YES;
}
因此,在SplashScreenViewController
中,添加指向您的应用appDelegate
的属性appDelegate。
然后,在你的闪屏上做你想做的事(登录,按钮等......)
在此示例中,这是一个简单的登录按钮。当您点击该按钮时,它会将一个功能调入您的appDelegate
- (IBAction)loginButton:(id)sender
{
[[self appDelegate]setMainTabBarControllerOnScreen];
}
此功能现在将加载tabBarController并删除splashcreen登录屏幕
self.tabController = [[UITabBarController alloc] init];
HomeViewController *hemView = [[HomeViewController alloc] init];
[hemView setTitle:@"Hem"];
hemView.tabBarItem.image = [UIImage imageNamed:@"icon_home.png"];
UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:hemView];
[homeNavigationController.navigationBar setTintColor:[UIColor colorWithRed:(0.96) green:(0.96) blue:(0.96) alpha:0.0]];
SearchViewController *sdkView = [[SearchViewController alloc] init];
[sdkView setTitle:@"Search"];
sdkView.tabBarItem.image = [UIImage imageNamed:@"icon_search.png"];
UINavigationController *sdkNavigationController = [[UINavigationController alloc] initWithRootViewController:sdkView];
[sdkNavigationController.navigationBar setTintColor:[UIColor colorWithRed:(0.96) green:(0.96) blue:(0.96) alpha:0.0]];
NSArray * arrayOfControllers = [[NSArray alloc] initWithObjects:homeNavigationController, sdkNavigationController, nil];
self.tabController.viewControllers = arrayOfControllers;
这就是诀窍
[[self window] addSubview:self.tabController.view];
[[self window]setRootViewController:[self tabController]];
[[[self viewController]view] removeFromSuperview];