Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How to create a Module in Xcode with a View for Titanium Mobile?

Hello,
i've studied the
http://developer.appcelerator.com/doc/mobile/iphone/module_sdk
but still did not managed to get a custom module with its own view to run.

Could anyone provide a simple example, please?
That would be helpful for the whole Titanium community, i guess ;)

Thank you in advance.

My specific problem:
My Module inherits from TiModule. To display a modal view is need to call presentModalViewController, but this method is only available if inherited from UIViewController. Anyone an idea, how to make this working for Titanium?

— asked June 21st 2010 by Martin Lasak
  • iphone
  • module
  • presentmodalviewcontroller
  • uiviewcontroller
  • view
1 Comment
  • It would seem that the whole of Appcelerator are out of the office for the last 6 weeks. It's quite annoying having to wait so long for an answer to a question that should be covered in any sort of reasonable documentation.

    — commented July 30th 2010 by Charlie Roche

4 Answers

  • Accepted Answer

    Finally figured this out. You don't have access to the current view controller, but you do have access to the app delegate, and it has a showModalController method that handles figuring out the view context for you. In this case, where "reader" was a view controller subclassed from UIImagePickerController, we used the following:

    [[TiApp app] showModalController:reader animated:YES];
    

    Note: we had to make sure the module (in ComDevelopmentnowTestModule.h) had implemented a couple protocols, like so:

    @interface ComDevelopmentnowTestModule : TiModule 
        <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
    

    And we had to have these lines before that in the function, to make sure that the function has access to the UI thread and the controller knows where to delegate.

    ENSURE_UI_THREAD(functionName,args);
    [reader setDelegate:self];
    

    Finally, to DISMISS the modal view when we got what we wanted, we had to do so by addressing it rather than the app:

    [reader dismissModalViewControllerAnimated: YES];
    

    You may not need all this if you're not doing the same thing we did. Good luck!

    — answered July 30th 2010 by Ben Strackany
    permalink
    3 Comments
    • Sorry for my late reply. Yes, this is the solution to our problem, we did quite the same approach some weeks ago when we tried to integrate a barcode scanner. Unfortunatelly the documentation of Module SDK wasn't sufficent in our case. After finsing the needle in the haystack we now have released a how-to for usage of the integrated Module as our prototype development TiBar.
      Thank you for your answer.

      — commented August 6th 2010 by Martin Lasak
    • Working perfectly… Main thig is in TiModule main file, you need to use [TiApp app] and for the method you are performing ViewController to load, call ENSURE_UI_THREAD

      — commented April 17th 2012 by Paresh Thakor
    • My code is not working on device, could anyone please help..?

      - (id) TweetPost : (id) args {
          NSArray *arr = args;
          _post = [args objectAtIndex: 0];
          NSLog(_post, nil);
          [_post retain];
          ENSURE_UI_THREAD(Tweet, args);
      }
      
      - (id) Tweet : (id) args {
              NSLog(@"Routine Twitter Module", nil)
              if (![_engine isAuthorized]) {
                  UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
                  if (controller) {
                      [[TiApp app] showModalController:controller animated: YES];
                  }
              } else {
                  [_engine sendUpdate: _post];
              }
      }
      

      While executing this code, on simulator, it works fine but on device the App crashes, instantly after NSLog(_post, nil); in TweetPost method. I think this is due to ENSURE_UI_THREAD(Tweet, args);. Can anyone help me out..?

      — commented April 23rd 2012 by Paresh Thakor
  • We're having exactly this problem right now–[self parentViewController] and [[TiApp app] parentViewController] aren't it, apparently. The documentation on ViewProxies is wildly out of date. We'd be grateful if anyone can provide an answer, and we'll definitely post one here ourselves if we figure it out.

    — answered July 29th 2010 by Ben Strackany
    permalink
    0 Comments
  • That guide is our most current documentation on SDK module development. If you're struggling to get a module running, you can try swinging by #titanium_dev on irc.freenode.net - this is the channel for people working on Titanium internals and extensions. You might be able to catch one of the devs there as well.

    — answered June 21st 2010 by Kevin Whinnery
    permalink
    0 Comments
  • Thank you for your answer Kevin. We will of course follow your advice and ask on irc. But nevertheless i try to precise my question, maybe to help some other developers as well.

    Question:
    How to get access to current viewcontroller from within TiModule for creating new subviews.

    Thank you.

    — answered June 21st 2010 by Martin Lasak
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.