2013-12-10 40 views
0

在運行我的應用程序時,在我的控制檯中需要有關「應用程序窗口在應用程序啓動結束時具有根視圖控制器」警告的幫助。這是我正在研究的核心數據測試。我沒有得到我用於測試的NSLog語句,只有前面的消息。在AppDelegate中實例化UITableViewController以與Storyboard配合使用iOS

我從空應用程序創建了一個新項目。我的應用程序委託didFinish方法代碼生成如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

我添加了一個故事板並將其設置爲主界面。然後添加一個UITableView到故事板。通過添加一個文件創建一個UITableViewController,並將其設置爲身份檢查器中的UITableView類。

它似乎擺脫了我應該設置rootViewController的警告。如果我沒有在appDelegate.m文件中實例化它,我該如何將我的UITableViewController設置爲rootViewController?或者,如果我做實例它在appDelegate.m像這樣

UITableViewController *tableViewController = [[UITableViewController alloc]init]; 
self.window.rootViewController = tableViewController; 

我怎麼用相應的.h和.m文件關聯tableViewController?

使用的Xcode 5.0.1,部署目標7.0

+1

糟糕的開發實踐。如果你不明白你在做什麼,從一個單一的視圖應用程序開始。 – duci9y

+0

這是因爲我不明白我在問這個問題。這就是爲什麼。 – noobsmcgoobs

回答

1

將故事板添加到空應用程序中,並將Info.plist中的屬性「主故事板文件基本名稱」設置爲故事板的名稱時,應用程序會實例化您的「窗口」對象並指定您的實例故事板的「initialViewController」作爲窗口對象的「rootViewController」屬性。所以你看不到警告:

「應用程序窗口預計將有在應用程序啓動的最後一個根視圖控制器」當你做:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions {  
    return YES; 
} 

這工作得很好。

然而,在代碼:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] 
    //Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

如果您使用的故事板,你已經通過創建一個新的窗口對象,它不再由故事板提供的RootViewController的覆蓋缺省行爲。在這種情況下,您必須明確地向您的窗口對象添加一個根視圖控制器。

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:[NSBundle mainBundle]]; 
    YourTableViewController* vc = (YourTableViewController*)[storyboard instantiateInitialViewController]; 

    _window.rootViewController = vc; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

希望這有助於!

+0

是的,謝謝你的完整答案! – noobsmcgoobs

0

在故事板設定所需的控制器initialViewController。

在AppDelegate.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    youRootViewControllerObject = [storyboard instantiateInitialViewController]; 

這樣,您就可以訪問YouRootViewController類。

+0

我沒有'self.window.rootViewController'代替你提到的你的RootViewControllerObject。它的工作,沒有與使用我的UITableViewController對象,除非我正確閱讀你的答案 – noobsmcgoobs

+0

它很好,你的問題解決了。 – Deepak

0

我做了很多研究,終於找到了在appDelegate.m做到這一點

,而不是

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary  *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

的代碼應該簡單地

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

    return YES; 
} 

看到合適的方法Sitepoint幫助頁面獲取更多詳細信息 http://www.sitepoint.com/ios-application-development-storyboards/

相關問題