在phonegap / iphone中加载自定义视图

时间:2011-11-21 10:33:09

标签: iphone cordova

我已经为iphone制作了一个正常工作的插件。我可以调用所有本机方法。我还制作了一个我要加载的自定义视图(.xib文件)。但我无法弄清楚如何使用phonegap做到这一点。

通常我会做类似的事情:

[self addSubView:myView2];

但这不起作用。

任何人都知道在使用phonegap时如何加载和显示自定义.xib文件?

1 个答案:

答案 0 :(得分:1)

到目前为止,您使用过Child browser plugin,此插件还包含ChildBrowserViewController.xib。因此,要在Phonegap中调用另一个xib,您需要修改- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType appDelgate.m文件中的方法。

在HTML和javascript代码中,您需要提供需要在shouldStartLoadWithRequest方法中处理的特殊链接(使用href或window.location方法),在appDelegate中导入#import MyCustomViewController.h .m文件。

请查看以下代码段 -

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    NSURL *url = [request URL];
    if([request.URL.absoluteString isEqualToString:@"about:blank"])
        return [ super webView:theWebView shouldStartLoadWithRequest:request
                navigationType:navigationType ];
    if ([[url scheme] isEqualToString:@"gap"]) {
        return [ super webView:theWebView shouldStartLoadWithRequest:request
                navigationType:navigationType ];
    } 
        else {
        NSString *urlFormat = [[[url path] componentsSeparatedByString:@"."] lastObject];
        if ([urlFormat compare:@"xib"] == NSOrderedSame) {
            [theWebView sizeToFit];
            MyCustomViewController* customVC = [ [ MyCustomViewController alloc ] initWithScale:FALSE ];
            customVC.modalPresentationStyle = UIModalPresentationFormSheet;
            customVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [super.viewController presentModalViewController:customVC animated:YES ];           
            [customVC release];
            //return YES;
            return NO;  
        } 
        else
            return [ super webView:theWebView shouldStartLoadWithRequest:request
                    navigationType:navigationType ];    
    } 
}

感谢,
MAYUR