2012-01-08 66 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