【问题标题】:Stopping UiWebView with a button使用按钮停止 UiWebView
【发布时间】:2013-01-17 12:46:24
【问题描述】:

现在我的项目自动重新加载(在此站点上的某人的帮助下 :D)我怎么能得到另一个但停止重新加载? 这就是我所拥有的:

-(IBAction)SendGPSData:(id)sender {
    NSURL *myURL =[NSURL URLWithString:@"http://www.google.co.uk"];//<<change this!!!
    NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
    [myWebView2 loadRequest:myRequest];
        [myWebView2 reload];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(WebViewLoad:) userInfo:nil repeats:YES];

}

-(IBAction)stop:(id)sender {

//what do i put here to stop it reloading?

}

-(void)WebViewLoad:(NSTimer *)theTimer
{
    [myWebView2 reload];

} 

不喜欢这么快问另一个问题,但我是 Xcode 的初学者,所以任何帮助都会很棒:)

我试过 myWebView 停止加载; 但原来的似乎覆盖了这个:(

【问题讨论】:

    标签: iphone xcode button webview


    【解决方案1】:

    你可以这样停下来:

    -(IBAction)stop:(id)sender {
    
    [webView stopLoading];
    
    }
    

    【讨论】:

      【解决方案2】:

      您还需要停止计时器。所以你这样做:

      NSTimer* reloadTimer;  // Put this in your class.
      
      reloadTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(WebViewLoad:) userInfo:nil repeats:YES];
      
      
      -(IBAction)stop:(id)sender
      {
          [reloadTimer invalidate];
          reloadTimer = nil;   // Prevents crash, when this method is called twice:
                               // reloadTimer is released when you call invalidate,
                               // so next time invalidate would be called on non-existing
                               // object -> crash.
      
          [myWebView2 stopLoading];
      }
      

      将启动/创建计时器的代码放在单独的方法中可能是有意义的,这样您就可以使用它重新启动重新加载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        相关资源
        最近更新 更多