2011-04-01 17 views
1

我是iPhone開發者的初學者,我正在開發我的第一批應用程序。實際上,我已經創建了一個Tab Bar應用程序,但是我想在運行應用程序時添加Splash Screen。TabBarController之前的開機畫面

我發現了一個問題,究竟在Loading a Welcome Screen(Splash Screen) before TabBarController

但是當我嘗試把我的代碼,閃屏不加載,只是繼續顯示我的tabbarcontroller。

我創建了一個SplashViewController.h,SplashViewController.m和SplashView.xib和下面是我的代碼,

#import "SplashViewController.h" 
... 
... 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
     // Override point for customization after application launch. 
     SplashViewController *controller = [[SplashViewController alloc] initWithNibName:@"SplashView" 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; 
    } 

的應用程序運行沒有錯誤,但只是無法加載啓動畫面,任何意見表示高度讚賞。謝謝!

+1

你的SplashScreenController是做什麼的?你在.xib中定義了任何可見的東西嗎?如果你想要一個靜態圖像,你可以命名id Default.png並把它放在你的包中,不需要其他代碼。 – Rayfleck 2011-04-01 14:20:49

+0

右鍵,在將圖像文件重命名爲Default.png後,在啓動應用程序時可以看到它,但我想保留它,直到用戶按下任何關閉的位置。任何建議?謝謝! – TonyNet 2011-04-01 14:39:37

回答

3

我的猜測是標籤欄控制器忽略了對presentModalViewController:animated:的呼叫,因爲它尚未顯示在屏幕上。嘗試將標籤欄視圖作爲子視圖添加到窗口後,將呼叫移至。即使在致電makeKeyAndVisible後,也可能發生此情況。

+0

是的,在makeKeyAndVisible下移動代碼之後,可以顯示啓動畫面。謝謝! – TonyNet 2011-04-01 15:00:02

0

我會建議只是將閃屏視圖控制器的視圖添加到您的窗口,並使其爲主。無需使用標籤欄控制器以模態方式呈現它。那麼在啓動畫面中只需一個按鈕就可以佔用整個屏幕,並且只要按下它就可以移除並釋放視圖並進行常規窗口設置(配置標籤欄等)。

編輯:一些代碼來說明我的意思,

在你的應用程序代理

- (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]; 
} 

在你飛濺視圖控制器:

-(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]; 
} 

有一點要注意:我假設你初始化和設置你的標籤欄控制器在一個筆尖(從你原來的帖子看來)。

+0

你有任何我可以參考的樣本嗎?因爲我不知道如何讓普通UIViewController調用另一個TabBarController視圖。我仍然不知道發展方式。 – TonyNet 2011-04-01 15:04:23

+0

您基本上會在viewController中使用您的單例應用程序委託[[UIApplication sharedApplication]委託],然後使用它的窗口添加標籤barController視圖,就像您現在一樣。沒什麼複雜。你甚至可以像你現在一樣在你的應用程序委託中加載tabBarController,然後將其視圖添加到窗口中。實際上,添加到我的答案與一些代碼來幫助。 – 2011-04-01 15:12:54

+0

好的,這意味着我可以在SplashViewController和tabBarController之間切換,就像兩個視圖一樣。我會盡力並感謝您的建議。 – TonyNet 2011-04-01 15:24:18

0

如果您的要求是在用戶點擊它之前顯示一個視圖,那麼MetaLik的建議將會起作用。 或者,您可以直接將閃屏控制器的視圖添加到應用程序窗口。

[self.window addSubview:MySplashController.view]; 

在這兩種情況下,你需要創建一個按鈕或UIResponder的一些子類,以用戶的敲擊響應,並且當你的是,無論是dismissModalViewController或[self.view removeFromSuperview],這取決於如何你實例化它。

+0

我試着使用splashViewController.view,但只是顯示主窗口屏幕。怎麼了?謝謝! – TonyNet 2011-04-01 15:16:51

+0

同樣,你的SplashViewController是做什麼的 - 它是背景顏色,它是什麼框架,你期望看到什麼? – Rayfleck 2011-04-01 15:24:46

0

來自Mike的關於如何設置啓動畫面的優秀評論,爲了使污垢變得簡單,啓動畫面創建一個名爲Default.png的靜態圖像,並且它的工作原理是magico。它應該最多顯示5秒或直到您的應用加載。我使用寬度x高度尺寸爲320x480,基本上正是我所需要的。

謝謝Mike。

而且,這裏是與創建一個啓動畫面和圖標等一起去一個有用的鏈接... Custom Icon and Image Creation Guidelines

0

我這樣做,它工作正常。

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];