【问题标题】:UIWebView just not workingUIWebView 只是不工作
【发布时间】:2014-02-07 12:17:42
【问题描述】:

我正在尝试让 Googles Chart Api 在 UIWebView 上工作,但我就是无法获得它!

我创建了 UIWebView,将委托设置为自行处理任何加载错误(使用 didFailLoadWithError 方法,顺便说一句,没有错误),以这种方式使用 html 字符串触发加载:

@interface GCAViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonGo;
@property (weak, nonatomic) IBOutlet UIWebView *webViewChart;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;

@end

@implementation GCAViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.webViewChart.delegate = self;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%@", error.description);
}

- (IBAction)buttonGoClick:(id)sender {
    NSString *str = @"<html>"
     "<head>"
     "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>"
     "<script type=\"text/javascript\">"
     "alert('t1');"
     "google.load('visualization', '1.0', {'packages':['corechart']});"
     "google.setOnLoadCallback(drawChart);"
     "function drawChart() {"
         "var data = new google.visualization.DataTable();"
         "data.addColumn('string', 'Topping');"
         "data.addColumn('number', 'Slices');"
         "data.addRows(["
                       "['Mushrooms', 3],"
                       "['Onions', 1],"
                       "['Olives', 1],"
                       "['Zucchini', 1],"
                       "['Pepperoni', 2]"
                       "]);"
         "var options = {'title':'How Much Pizza I Ate Last Night',"
             "'width':400px,"
             "'height':300px};"
         "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));"
         "chart.draw(data, options);"
     "}"
     "</script>"
     "</head>"

     "<body>"
     "<div id=\"chart_div\"></div>"
     "</body>"
     "</html>";
    [self.webViewChart loadHTMLString:str baseURL:nil];

}

@end

什么都没有发生!没有错误,没有消息,什么都没有!

谁能帮忙?

额外信息:这是一个测试应用程序,旨在仅执行此操作...

编辑 1: 经过一些调试后,我得到了一个修复,该错误与函数 DrawChart 有关。如果我删除函数声明,alert('t1') 会发生就好...

【问题讨论】:

  • 你的 self.webViewChart 的值(地址)是多少?
  • @bsarr007 它是 UIWebView 本身...我更新并发布了所有代码...
  • 您是否检查了 Interface builder 上的 IBOutlet,webview 插座是否连接到属性?
  • @Malloc 是的,它已正确连接...我可以更改其他属性并加载更简单的 html ("Hello world!")
  • 你能不能实现webViewDidStartLoad:委托方法,在里面设置一个断点,看看它是否被调用

标签: ios objective-c uiwebview google-visualization


【解决方案1】:

只需用引号将宽度/高度括起来

"'width':'400px',"
"'height':'300px'};"

【讨论】:

    【解决方案2】:

    我明白了...在函数中,它不是 300px/400px -> 它不能在末尾有 px...奇怪...

    【讨论】:

    • 我必须 NSLog 你的 str 并在浏览器中运行输出以查找错误。你比我快了几秒钟,但感谢你接受答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2018-04-20
    • 2013-05-08
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多