1

我在使用 .xib 实例化 UIViewController 时遇到问题。

我的代码如下所示:(实际上,只需看这里:https ://github.com/mrtnbroder/titanium-module-test )

- (void)open:(id)args
{
    ENSURE_UI_THREAD(open, args);

    CustomViewController *viewController = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    [[TiApp app] showModalController:navigationController animated:YES];
}

我的 .xib 文件名为 CustomViewController.xib

看起来像这样:

TestCamNavigationController.xib

但是,当我构建它时,我的应用程序如下所示:

iPhone 模拟器

为什么?怎么了?

4

4 回答 4

1

问题是您的笔尖不在主包中。您应该首先将所有笔尖复制到module_folder /assets 的文件夹中。

将该文件夹重命名为 Titanium.bundle。

所以你的目录结构看起来像module_folder /assets/Titanium.bundle

这将确保您的 nib 在编译时被复制到模块中。

我在 NSBundle 上创建了一个这样的类别:

//Shortcut if you want to name the bundle Titanium you'll be ahead of the game
+ (NSBundle *)titaniumBundle
{
    return [NSBundle titaniumBundleWithName:@"Titanium"];
}

//Bundles are put in a folder called modules inside the application's main bundle
+ (NSBundle *)titaniumBundleWithName:(NSString *)bundleName
{
    //Change this to accommodate the module's id
    static NSString *appId = @"com.companyname.module";
    NSString *bundlePath = [NSString stringWithFormat:@"modules/%@/%@.bundle", appId, bundleName];
    bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:bundlePath];

    return [NSBundle bundleWithPath:bundlePath];
}

然后,您将在您正在使用的文件中包含该类别并对其进行编码:

 CustomViewController *viewController = 
   [[CustomViewController alloc] initWithNibName:@"CustomViewController"
                                          bundle:[NSBundle titaniumBundle]];

然后你至少会从正确的目录加载视图。

于 2014-12-01T19:22:05.627 回答
0

这一行:

  CustomViewController *viewController = 
       [[CustomViewController alloc] initWithNibName:@"CustomViewController"
                                              bundle:nil];

建议您应该调用您的 xib 文件“CusomViewController.xib”。

更新

通过您的问题编辑(和您的评论),您表明您正在为 Appcelerator Titanium 开发一个模块。

据我所知,Titanium 不使用 Xib 文件或视图控制器(而是使用“视图代理”)。根据这个答案,在 Titanium 中使用 Xib 文件是一个非常复杂的过程:

您需要将您的 XIB 转换为 NIB。最简单的方法是将XIB添加到本机项目,编译项目,然后拉出NIB。将其转储到模块的资产中,然后从模块代码中引用它。...请注意,我们通常不使用 NIB,除非我们有来自第三方的东西迫使我们这样做。以命令方式创建视图比以声明方式创建视图更容易。

由于我只知道原生而不是 Titanium,因此我无法进一步帮助您。但我建议您遵循 Titanium 文档,不要将其与原生 iOS 开发混淆。

于 2013-08-15T10:49:22.053 回答
0

你在做sotryboard吗?

如果不是检查你的didFinishLaunchingWithOptions方法AppDelegate

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
于 2013-08-15T09:52:14.893 回答
0

首先在你的 xxxmodule.m

    -(void)startup
    {
        // this method is called when the module is first loaded
        // you *must* call the superclass
        [super startup];

        yourViewController *controller = [[yourViewController alloc] init];
        controller.delegate = self;
[[TiApp app] showModalController: controller animated: YES];

    }
于 2013-08-16T14:09:34.000 回答