stringByEvaluatingJavaScriptFromString 的奇怪问题不适用于 NSUSerDefault 值...请帮忙!

发布于 2024-10-02 14:17:27 字数 992 浏览 5 评论 0原文

我正在为我拥有的图书阅读器 iOS 应用程序开发书签功能。按照现在的设置方式,用户滚动浏览视图,当他们想要保存或在离开之前为他们的位置添加书签时,他们可以点击底部工具栏上的保存按钮。

当他们点击此保存按钮时,保存的操作将被调用:

-(IBAction) savePlace:(id)sender{

     int pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
     NSLog(@"%d Set Y Value", pageYOffset);
     [[NSUserDefaults standardUserDefaults] setSavedSpot:pageYOffset];

}

令人惊讶的是,我已经让这部分工作了。我可以点击保存按钮,控制台会读取 200 表示我在屏幕上的位置,然后当我离开并返回时,由于 NSUSerDefaults 调用的这个方法,200 再次打印到控制台应用程序加载:

- (void) setCurrentSpot:(NSUInteger)ySpot {

       NSLog(@"%d Saved Y Value", ySpot);

         [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @"window.scrollTo(0, %d);", ySpot]];
}

但是!什么也没发生...我知道我正在保存并正确检索正确的 Y 轴值,但是当调用该 JavaScript 方法时,它不会触发。

为了使事情变得更加复杂,我继续制作了一个自定义 IBAction 来访问相同 ySpot 值,并使用完全相同的 JavaScript 方法将视图移动到位置 200,并且它工作完美!

我缺少什么?我不明白发生了什么事。谢谢

I am working on a bookmarking feature for a book reader iOS app I have. The way it is setup now, the user scrolls through a view, and when they want to save, or bookmark their spot before they leave, they hit a save button on the bottom toolbar.

When they hit this save button, the saved action is called:

-(IBAction) savePlace:(id)sender{

     int pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
     NSLog(@"%d Set Y Value", pageYOffset);
     [[NSUserDefaults standardUserDefaults] setSavedSpot:pageYOffset];

}

Surprisingly enough, I have got this part working. I can hit the save button, the console will read say 200 for where I'm at on the screen, and then when I leave and come back, 200 is again printed out to the console thanks to this method that is called by NSUSerDefaults when the app loads:

- (void) setCurrentSpot:(NSUInteger)ySpot {

       NSLog(@"%d Saved Y Value", ySpot);

         [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @"window.scrollTo(0, %d);", ySpot]];
}

BUT! Nothing happens...I know for a fact that I am saving and correctly retrieving the correct Y-axis value, but when that JavaScript method is called, it won't fire.

To further complicate things, I went ahead and made a custom IBAction that accesses that same ySpot value, and used the exact same JavaScript method to move the view to position 200, and it works perfectly!

What am I missing? I don't see what is going on. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2024-10-09 14:17:27

编辑:误读了你的问题,

也许你调用的 javascript 调用方法调用得太快了?并且不给 UIWebview 时间来初始化和反应?如果当你等待网络视图出现然后调用它时它有效,那就是我唯一能看到的东西。

edit: misread your question,

Perhaps the method you are calling to call that javascript is being called too fast? and not giving the UIWebview time to initialize and react? if it works when you wait on the webview to come upand then call it, that is the only thing i could see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文