2011-06-08 187 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];