titaniumuipopovercontrollertitanium-mobiletitanium-modulestitanium-proxy

Titanium Module: Passing Titanium.UI.Button into module to display UIPopoverController


I am developing a module which will work for both iPhone and iPad. In this module, I am trying to mimic UIActivityViewController and will display custom for both iPhone and iPad.

Problem: The Popover is displayed at top, left corner in iPad screen. The button from which I display Popup is created into Titanium SDK and I am passing the same Button as parameter into the module, which in turn, will use the same to display Popover at particular position.

Please check my code below: [The code is much bigger than the pasted code but I have simplified the code and pasted only the basics showing how my code works.]

Titanium Code:

    root = {};
    var w = Titanium.UI.createWindow({
        top : 0,
        left : 0,
        right : 0,
        bottom : 0,
        navBarHidden : true,
        orientationModes : [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT]
    });
    w.open();

    var shareItButton = Titanium.UI.createButton({
         top : 10,
         right : 40,
         width: 90,
         height: 30,
         backgroundImage : 'images/shareit.png'
    });
    baseview.add(shareItButton);

    // Display Popover when shareItButton is clicked
    shareItButton.addEventListener('click', function(e) {
        var ShareView = require('com.xyz.sharing');
        root.shareController = ShareView.shareViewController; // The proxy controller

        root.shareController.shareContent({
           message: "Share Message",
           url: "http://www.google.com",
           title: "Share Title",
           file: "File path for iBooks functionality",
           shareButton: e.source // e.source is actually a button
        });
    });

Titanium Module: com.xyz.sharing

Module.h

#import "TiModule.h"

@interface ComXYZSharingModule : TiModule {

}

- (id) shareViewController;
@end

Module.m

@implementation ComXYZSharingModule
// Ignoring built-in methods, I have pasted only Custom methods
- (id) shareViewController {
    ComXYZSharingProxy *proxy = [[ComXYZSharingProxy alloc] init];
    proxy.module = self;
    return proxy;
}
@end

Proxy.h

@interface ComXYZSharingProxy : TiProxy

@property (nonatomic, strong) ComXYZSharingModule *module;
@property (nonatomic, strong) NSDictionary *content;
@property (strong, nonatomic) TiViewProxy *exportView;

- (void) shareContent : (id) args;
@end

Proxy.m

@implementation ComXYZSharingProxy
    - (void) shareContent : (id) args {
        ENSURE_UI_THREAD(shareContent, args);
        NSArray *val = args;
        NSDictionary *dict = [val objectAtIndex: 0];

        self.content = dict;
        if ([dict objectForKey: @"shareButton"]) {
            self.exportView = [dict objectForKey: @"shareButton"]; // This is the Button which gives me wrong values
            NSLog(@"Button Found", nil);
            NSLog([self.exportView description], nil);

            CustomActivityController *controller = [[CustomActivityController alloc] init];  // We have just created the controller which is just customization of UIActivityViewController and that's the main view which will be displayed in Popover for iPad and in modal control for iPhone

            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
            //[self setPopoverController:popover];
            //[popover setDelegate:self];
            [popover setBackgroundColor: [UIColor greenColor]];
            popover.popoverContentSize = CGSizeMake(320, 250);
            CGRect rect = [TiUtils rectValue: [self.exportView rect]];

            NSLog([NSString stringWithFormat: @"Size: x: %f,y: %f, width: %f, height: %f", rect.origin.x, rect.origin.y
       , rect.size.width, rect.size.height], nil);

            [popover presentPopoverFromRect:rect inView:[[TiApp app] controller].view permittedArrowDirections:arrowDirections animated:animated];
        }
    }
@end

Output when Program runs

Button Found
Size: x: 0.00,y: 0.00, width: 0.00, height: 0.00 [This should be actual size of Button but somehow it's not]

Due to this problem my Popover displays at top left corner of the iPad screen. Anyone would help to solve the issue? If I try to use TiUIButton or TiUIButtonProxy then also the result is same.


Solution

  • I just replaced following code

    [popover presentPopoverFromRect:rect inView:[[TiApp app] controller].view permittedArrowDirections:arrowDirections animated:animated];
    

    with

    [popover presentPopoverFromBarButtonItem:[view barButtonItem] permittedArrowDirections:UIPopoverArrowDirectionAny animated: animated];
    

    in shareContent function and this resolved my issue.