【问题标题】:Three20 display local html fileThree20显示本地html文件
【发布时间】:2011-06-07 03:25:39
【问题描述】:

我正在尝试显示来自 three20 表控制器的本地 html 文件。表格显示正确,但是当我选择项目时,我得到的只是一个空白屏幕。如果我使用外部 URL,页面会正确显示。我没有尝试过使用 UIWebView,但我想知道是否有使用 Three20 框架的简单方法。

谢谢。

【问题讨论】:

    标签: html ios three20


    【解决方案1】:

    编辑:我最近忘记了that has changed...

    假设您以这种方式在导航器中注册了TTWebController

    TTNavigator* navigator = [TTNavigator navigator];
    TTURLMap* map = navigator.URLMap;
    [map from:@"*" toViewController:[TTWebController class]];
    

    您可以通过这种方式简单地打开一个本地 html 文件:

    NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:creditsPath];
    TTOpenURL([url description]);
    

    那么您所要做的就是将TTTableLinkedItem 或子类(几乎所有 TT*Cell 都是)添加到您的数据源中,并使用这样的本地文件 URL:

    NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:creditsPath];
    TTTableTextItem *item = [TTTableTextItem itemWithText:@"foobar" URL:[url description]];
    

    比我之前写的简单多了……

    ENDOFEDIT

    忘记下面的内容,除非您对其他解决方案感兴趣...(更复杂...与否,请参阅最后一个)


    基本上这是您在UIWebView 中加载内容的方式:

    NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:creditsPath];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [webView loadRequest:urlRequest];
    

    three20TTWebController 可以在查询中接受请求:

    - (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
      if (self = [self initWithNibName:nil bundle:nil]) {
        NSURLRequest* request = [query objectForKey:@"request"];
        if (nil != request) {
          [self openRequest:request];
        } else {
          [self openURL:URL];
        }
      }
      return self;
    }
    

    因此,要在 TTWebController 中打开本地 html 文件,您可以这样做:

    NSString *creditsPath = [[NSBundle mainBundle] pathForResource:@"credits" ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:creditsPath];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [[TTNavigator navigator] openURLAction:
     [[[TTURLAction actionWithURLPath:@"url://to/your/ttwebcontroller"]
       applyQuery:[NSDictionary dictionaryWithObject:ulrRequest
                                              forKey:@"request"]]
      applyAnimated:YES]];
    

    现在是最后一部分,从TTTableViewController 触发它... 你有在你的ViewController 来实现:

    - (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
      // there you retrieve a *TTTableItem* corresponding to your row
      // and call previous snippet from URL...
      // TTTableItem class has userInfo property you could use:
      TTTableItem *item = (TTTableItem *)object;
      NSString *htmlFile = item.userInfo.
      [self openLocalHtmlFile:htmlFile];
    }
    

    希望对您有所帮助! 没有经过测试的东西,根本不应该编译或任何东西,但这是一个解决方案。

    这是一个使用基本 three20 + iOS 功能的解决方案。您还可以编写一个继承自 TTWebControllerTTCustomWebController,这将采用像 @"myapp://openLocalHtml?file=credits" 这样的网址,这并不难......

    ...这是草稿:

    @interface TTCustomWebController : TTWebController {}
    @end
    
    @implementation TTCustomWebController
    
    - (id)initWithFile:(NSString *)name {
      self = [super init];
      if (self) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"html"];
        NSURL *URL = [NSURL fileURLWithPath:filePath];
        [self openURL:URL];
      }
      return self;
    }
    
    @end
    

    然后你可以简单地在你的数据源中使用TTTableLinkedItem 指向这些@"myapp://openLocalHtml?file=credits"...

    祝你好运! :)

    【讨论】:

    • 非常感谢!我会告诉你进展如何!
    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多