代码之家  ›  专栏  ›  技术社区  ›  Mc.Lover

在一个UIWebView上加载不同的HTML文件

  •  0
  • Mc.Lover  · 技术社区  · 14 年前

    我要换我的 UIWebView 文件通过 UISwipeGuesture 但有问题:

    我在uiwebview上加载了100个HTML文件,所以我希望实现如下内容: 如果用户滑动视图,我的WebView将加载下一个HTML文件,但问题是我的代码只加载第一个HTML文件:

    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"file%d",+1] ofType:@"html"];
    NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    
    NSString *htmlString = [[NSString alloc] initWithData: 
                            [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
    
    
    [self.myWebView loadHTMLString:htmlString baseURL:nil];
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Kris Markel    14 年前

    [NSString stringWithFormat:@"file%d",+1] 总是返回字符串 file1 ,所以你总是在加载 file1.html .

    你的意思是使用计数器或索引而不是数字1?

    编辑:更多代码。这不是我建议的实现方法,但是它应该给你一个概念,而且它相对紧凑。(我将使用ivar或属性跟踪当前文件索引。)

    static NSInteger currentIndex = 0;
    if (currentIndex < 99) {
        currentIndex++;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"file%d",currentIndex] ofType:@"html"];
    NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    NSString *htmlString = [[NSString alloc] initWithData: 
                            [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
    [self.myWebView loadHTMLString:htmlString baseURL:nil];
    
    推荐文章