1

I'm having problems instantiating a UIViewController with it's .xib.

My Code looks like this: ( actually, just look here: 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];
}

and my .xib file is named CustomViewController.xib

and looks like this:

TestCamNavigationController.xib

however, when I build it, my App looks like this:

iPhone Simulator

why? what's wrong?

foundry
  • 31,615
  • 9
  • 90
  • 125
Martin Broder
  • 325
  • 1
  • 7
  • 21

4 Answers4

1

The problem is that your nib is not in the main bundle. You should first copy all your nibs into a folder into module_folder/assets .

Rename that folder something like Titanium.bundle.

So your directory struction will look like module_folder/assets/Titanium.bundle

This will make sure your nib gets copied into the module when it gets compiled.

I make a category on NSBundle like this:

//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];
}

Then you're going to include that category in the file you're using and code this up:

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

Then you'll at least be loading the view from the right directory.

RyanL
  • 108
  • 1
  • 8
  • nice catch with the category :) i need to emphasis on the fact that the nib file needs to be bundled and not the xib. – TheFuquan Jan 29 '15 at 08:17
0

are you doing with sotryboard??

if not than check your didFinishLaunchingWithOptions method in AppDelegate

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • I don't use a storyboard. Also, I am developing a module for Appcelerator Titanium (https://tinyurl.com/of8cnwm) so I don't have any access to `didFinishLaunchingWithOptions`. You can view the whole project here: https://github.com/mrtnbroder/titanium-module-test – Martin Broder Aug 15 '13 at 10:00
0

This line:

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

suggests you should call your xib file "CusomViewController.xib".

update

With your question edit (and your comments) you reveal that you are developing a module for Appcelerator Titanium.

From what I can gather, Titanium does not use Xib files or viewControllers (it uses 'View Proxies' instead). According to this answer, using Xib files in Titanium is a very convoluted process:

You need to convert your XIB to a NIB. The easiest way is to add the XIB to a native project, compile the project, and then pull out the NIB. Dump it in the assets for the module, and then reference it from your module code. ... Note that we usually don't use NIBs unless we have something from a third party that forces us to. It's easier just to create the views imperatively rather than declaratively.

As I only know native, not Titanium, I cannot help you further with this. But I suggest that you follow the Titanium documentation, and don't confuse it with native iOS development.

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
  • I'm sorry, I don't get what you mean. You suggest calling `initWithNibName:@"CustomViewController.xib"`? – Martin Broder Aug 15 '13 at 10:56
  • No. Call initWithNibName:@"CustomViewController" but change the name of the xib file (currently named TestCamNavigationController.xib) to "CusomViewController.xib". OR alternatively... don't change the name of the xib file but change the code to initWithNibName:@"TestCamNavigationController". But that name seems a little misleading as it is a custom view controller's xib, not a navigation controller's xib. – foundry Aug 15 '13 at 11:05
  • Doesn't work. Changed the name, but I still get the same screen as shown above. Updated the Code at Github. – Martin Broder Aug 15 '13 at 11:11
  • @MartinBroder -see my update. Titanium UI development differs too much from native iOS development for me to be able to help further. – foundry Aug 16 '13 at 13:07
0

First of all in your 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];

    }
Krishna Kumar
  • 1,652
  • 9
  • 17
  • Sorry, but what Delegate Protocol do you mean? And why `init` and not `initWithNibName`? – Martin Broder Aug 19 '13 at 13:34
  • yes you can initWithNibName. You have to set delegate for controller in your module. Because your module is linked to your titanium and we need to set delegate for that. Did you try this code? Does it work? – Krishna Kumar Aug 19 '13 at 13:39
  • I tried it. Does not work. `[ERROR] Script Error Could not load NIB in bundle: 'NSBundle (loaded)' with name 'CustomViewController'` – Martin Broder Aug 19 '13 at 15:02