2012-01-08 68 views
4

我的應用程序是一個基於視圖的應用程序。我需要創建一個UINavigationController作爲我的rootViewController使用Xcode 4.2的Interface Builder中的UIApplicationDelegate

在Xcode中的前一版本,有一個名叫mainWindow一個XIB文件中,我們可以:

  • 我們UIApplicationDelegate實現連接到UIApplicationdelegate出口
  • 連接一個UIWindowUIApplicationDelegate
  • UIViewController連接到UIWindowrootViewController屬性。

但現在(Xcode 4.2)不會創建這個xib文件!

那麼如何創建自定義UINavigationController並將其連接到InterfaceBuilder中的UIApplicationDelegate實現?

這裏是我的代碼在我UIApplicationDelegate實現:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    SDWebImageRootViewController *root = [[SDWebImageRootViewController alloc] initWithNibName:nil bundle:nil]; 

    _navigationController = [[[UINavigationController alloc] initWithRootViewController:root] autorelease]; 

    self.window.rootViewController = _navigationController; 

    [[_navigationController navigationBar] setBarStyle:UIBarStyleBlack]; 
    [[_navigationController navigationBar] setTranslucent:YES]; 

    [_window addSubview:[_navigationController view]]; 
} 

回答

8

首先,是的,你仍然可以甚至在Xcode的最新版本中使用IB對於這一點,所以我不知道你在哪裏」從那裏重新獲得。

如果你想知道如何在不IB指定您的應用程序委託,這是相當簡單:

在你main方法,簡單地使用你的應用程序委託的類名作爲第4個參數UIApplicationMain

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool 
    { 
     int retVal = UIApplicationMain(argc, argv, nil, @"APPLICATION_DELEGATE_CLASS_NAME"); 
     return retVal; 
    } 
} 

事實上,當你從模板創建一個基於視圖的應用程序時,默認情況下,Xcode 4.2會爲你做這件事(儘管它沒有使用靜態字符串......這可能比我的建議誠實,因爲它會得到重構如果你使用內置的重構等):

NSStringFromClass([AppDelegate class])

要回答你的後續問題:ok , then what should I do ? to connect the UINC outlet in interface ?

不要打擾。

還是不相信我?好的...這是一個教程...它是十五步! ...並且毫無意義。

首先,創建應用程序:

enter image description here

打開了main.m和替換nil第四個參數:

enter image description here

創建一個新的廈門國際銀行,或劫持現有(我要在這裏完成),然後創建文件所有者類UIApplication

enter image description here

接下來,添加從工具箱的Object和類更改爲UIApplicationDelegate子類:

enter image description here

現在,回到在切換到文件的所有者和delegate出口連接到您添加了UIApplicationDelegate。如果你正在做我所做的並劫持現有的xib,請刪除view參考。

enter image description here

現在從工具箱中添加UIWindow

enter image description here

現在添加了「的UIViewController from the toolbox. This is your custom的UIViewController . If you want a UINavigationController的or UITableViewController`只想補充一點吧。

enter image description here

如果使用自定義UIViewController類,在這裏指定的類:

enter image description here

UIWindowrootViewController出口連接到您的UIViewController

enter image description here

破解打開您的UIApplicationDelegate界面並設置或修改window屬性使其成爲IBOutlet

enter image description here

切換到執行和刪除任何「不值錢」的代碼,設置你的窗口和根視圖控制器。 (我在諷刺在這裏......這個簡潔的代碼做的一切,我們在這裏做...只是編程而不是通過IB)

enter image description here

切換回您的廈門國際銀行和聯播您UIApplicationDelegatewindow outlet。現在

enter image description here

,在你的目標的部署方式,將您的廈門國際銀行的 「主界面」:

enter image description here

完成.... pshew!

+2

+1,但我擔心你所描述的具有諷刺意味的將是真實的。 :( – Abizern 2012-01-08 22:46:19

+0

非常感謝!我希望在開始擴展應用程序之前做到這一點,我做了你所做的事情,但是2期:1 - 當我將文件所有者的類更改爲UIApplication時,一個交戰圖標出現在我的網點和行動,2我添加NSObject和...仍然不承認我的應用程序委託代理店 – 2012-01-08 22:48:32

+1

@Mc。情人1 - 是的,因爲這些不是UIApplication的商店,它們是你自定義視圖控制器的出口。擺脫他們。您必須將它們連接到您在後面的步驟中添加的視圖控制器。 2-添加NSObject之後,必須將類更改爲指向您的應用程序委託。仔細閱讀說明並嘗試瞭解您在每一步中所做的事情。 – Steve 2012-01-08 22:52:47