2011-06-08 139 views
0

我是iOS开发新手。我有一个启动画面,并希望用初始视图替换它,我的意思是一个图像和两个按钮。我如何设置初始视图?我使用的Xcode 4的iOS 4,这里是我的代码...iOS初始屏幕按钮

- (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,请帮助。

+3

最简单的方法是使用Xcode的集成Interface Builder。如果您是Xcode的新手,请遵循Apple的开发者教程。 – BoltClock 2011-06-08 10:27:49

回答

0

(警告,基于ARC代码...不要忘记释放) 我已经做了这样的:

首先,我把两个SplashScreenViewController*controllertabController作为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];