ios - Xcode UIWebView 不会更改 URL 已更改的页面

标签 ios objective-c xcode uiwebview

我根据屏幕上平移手势的距离自动生成 NSURL(长话短说......)。无论如何,我在生成 URL 时没有任何问题。 URL 动态变化,然后我有一个每秒重复一次的 NSTimer,它告诉 UIWebView 使用新 URL 重新加载。

但是,尽管据报告 URL 正在更改,但 UIWebView 将仅显示发送给它的第一个 URL。我还在监视网站本身,它每秒都会收到第一个 URL,而不是更新的 URL。我的代码现在非常简单,所以我不确定我做错了什么?任何帮助将不胜感激。

在 ViewController.h 中:

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property float slicenumber;
@property NSURL *dynamicUrl;

在 ViewController.m 中:

- (void)viewDidLoad {
  [super viewDidLoad];
  NSString *fullURL = @"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=.50&size=native&fmt=png";
  NSURL *url = [NSURL URLWithString:fullURL];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  [self.webView loadRequest:requestObj];
  [self.webView reload];
  self.webView.scrollView.scrollEnabled = NO;
  self.webView.scrollView.bounces = NO;
  self.slicenumber=.5;
  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UpdateSlice) userInfo:nil repeats:YES];}

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
  CGPoint translation = [recognizer translationInView:self.view];
  [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

  self.slicenumber=self.slicenumber+translation.y/350;
  if (self.slicenumber<0) {
    self.slicenumber=0;
  }
  if (self.slicenumber>1) {
    self.slicenumber=1;
  }
  self.sliceloc.text= [NSString stringWithFormat:@"%.2f",self.slicenumber];
  NSString *fullURL2 = [NSString stringWithFormat:@"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=%.2f&size=native&fmt=png",self.slicenumber];
  self.dynamicUrl = [[NSURL alloc] initWithString:[fullURL2 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
  NSLog(@"urlString = %@",self.dynamicUrl);}

- (void)UpdateSlice {
  [self.webView loadRequest:[NSURLRequest requestWithURL:self.dynamicUrl]];
  [self.webView reload];}

非常感谢!如果您需要任何其他信息,请告诉我。

最佳答案

[self.webView reload] - 将重新加载当前页面。这可能发生在 loadRequest 完成之前。 尝试删除此行。

此外,@joern 的评论是正确的; “事件”是用户做出平移手势。失去计时器。

关于ios - Xcode UIWebView 不会更改 URL 已更改的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006352/

相关文章:

ios - 在无效的 indexPath 处捕获了对 rect 的 NSInternalInconsistencyException 请求

iOS - Xcode 中的 JPG "open as"十六进制

objective-c - 如何实现字符串 SCAudio Stream? - 声云

ios - XIB 文件和模拟器之间没有关联

java - 有没有与 Android 的 Xcode clang 静态分析器相当或相似的东西?

ios - 如何从 objective-c 中的动态字符串中提取子字符串?

iOS:等到用户完成输入然后发送请求

ios - 让 Button 标题标签大小根据 swift 中的设备(iPad 或 iPhone)

ios - 标签栏项目不显示

objective-c - 如何使网页 View 的一部分稍微半透明?