2013-10-21 118 views
1

我正在設置將出現在應用程序啓動上的第一個viewController。這是我的AppDelegate.h:應用程序啓動後的黑屏

#import "AppDelegate.h" 
#import "TutorialController.h" // a simple UIViewController 

@implementation AppDelegate 

@synthesize window; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    window.rootViewController = [[TutorialController alloc] init]; 

    [window makeKeyAndVisible]; 

    return YES; 
} 

它不給任何警告,但啓動應用程序,啓動畫面之後,只出現黑屏。沒有這個代碼,一切正常。我不能在StoryBoard中這樣做,因爲在解決這個問題之後,我必須添加其他東西......有什麼可能是錯誤的?謝謝!

解決:Solved using followben's reply.

+0

用'window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen] .bounds]創建一個窗口;'在設置rootViewController之前? – SolidSun

+0

不,我剛剛初始化@屬性(強,非原子)UIWindow *窗口;'在.h但是,添加您的字符串,問題仍然存在... –

+0

一個屬性無法在頭中初始化。一個頭只能聲明東西。免責聲明:我知道有例外,但這是簡化的道理 –

回答

8

.H:

@class MainViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong,nonatomic) MainViewController *mainViewController; 

.M:

#import "MainViewController.h" 

@implementation AppDelegate 
@synthesize mainViewController; 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; 
    self.window.rootViewController = self.mainViewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

編輯,具有完整project link (zipped)(工作:))

EDIT2:

如果您正在使用Storyboard,製作故事板文件,並將其分配到項目的故事板,就像您在此圖片中看到的一樣:主界面與您的故事板文件名稱相同。

enter image description here 和你沒有寫任何東西到你的應用程序代理:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    return YES; 
} 

您可以通過移動這個小箭頭,一個視圖控制器,你要設置像起動設置起始頁此圖:

enter image description here

,如果你想與你的新TutorialViewController分配您的ViewController(在故事板)使它像這樣

enter image description here

+0

'無法在捆綁中加載NIB:'NSBundle –

+0

因此您沒有名爲「MainViewController」的XIB = NIB,則使用self.mainViewController = [[MainViewController alloc ] 在裏面]; – incmiko

+0

謝謝,但它運行並給出相同的黑屏... –

1

直到我所知,這個問題會發生由於- (空)的loadView方法。因此,刪除或註釋此方法,而是使用 - (void)viewDidLoad加載所有內容。

相關問題