【问题标题】:Appcelerator Module - Instantiate UIViewController with .xib?Appcelerator 模块 - 使用 .xib 实例化 UIViewController?
【发布时间】:2013-08-15 09:38:24
【问题描述】:

我在使用 .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

看起来像这样:

但是,当我构建它时,我的应用看起来像这样:

为什么?怎么了?

【问题讨论】:

  • 你使用 Storyboard 了吗?
  • 我已经添加了一个 Storyboard 文件,但后来删除了它。为什么?这有什么关系吗?
  • 是的,如果您将项目作为 Storyboard 模板启动,默认情况下它将首先查找 Storyboard 并将根视图设置为 Storyboard 的入口视图。

标签: ios objective-c xcode appcelerator titanium-modules


【解决方案1】:

问题是您的笔尖不在主包中。您应该首先将所有 nib 复制到 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]];

那么你至少会从正确的目录加载视图。

【讨论】:

  • 很好地抓住了这个类别:) 我需要强调一个事实,即需要捆绑 nib 文件而不是 xib。
【解决方案2】:

你在做sotryboard吗?

如果不是在AppDelegate 中检查您的didFinishLaunchingWithOptions 方法

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

【讨论】:

【解决方案3】:

这一行:

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

建议您应该将您的 xib 文件称为“CusomViewController.xib”。

更新

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

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

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

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

【讨论】:

  • 对不起,我不明白你的意思。你建议打电话给initWithNibName:@"CustomViewController.xib"?
  • 没有。调用 initWithNibName:@"CustomViewController" 但将 xib 文件的名称(当前名为 TestCamNavigationController.xib)更改为“CusomViewController.xib”。或者...不要更改 xib 文件的名称,而是将代码更改为 initWithNibName:@"TestCamNavigationController"。但这个名字似乎有点误导,因为它是自定义视图控制器的 xib,而不是导航控制器的 xib。
  • 不起作用。更改了名称,但我仍然得到与上图相同的屏幕。在 Github 上更新了代码。
  • @MartinBroder - 查看我的更新。 Titanium UI 开发与原生 iOS 开发差异太大,我无法提供进一步帮助。
【解决方案4】:

首先在你的 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];

    }

【讨论】:

  • 抱歉,您指的是什么委托协议?为什么是init 而不是initWithNibName
  • 是的,你可以用 NibName 初始化。您必须在模块中为控制器设置委托。因为您的模块已链接到您的钛,我们需要为此设置委托。你试过这个代码吗?有用吗?
  • 我试过了。不工作。 [ERROR] Script Error Could not load NIB in bundle: 'NSBundle </Users/mbroder/Library/Application Support/iPhone Simulator/6.1/Applications/2986965D-2584-4977-9636-ECA29466A48C/TestCam.app> (loaded)' with name 'CustomViewController'
猜你喜欢
  • 1970-01-01
  • 2016-02-22
  • 2014-12-17
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2016-11-07
  • 1970-01-01
相关资源
最近更新 更多