简体   繁体   English

stringByEvaluatingJavaScriptFromString的奇怪问题无法与NSUSerDefault值一起使用...请帮助!

[英]Strange problem with stringByEvaluatingJavaScriptFromString not working with NSUSerDefault value…please help!

I am working on a bookmarking feature for a book reader iOS app I have. 我正在为我拥有的读书器iOS应用程序添加书签功能。 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: 我可以单击“保存”按钮,控制台将在屏幕上显示200,然后我离开并返回时,由于NSUSerDefaults调用此方法,因此再次将200打印到控制台。该应用程序加载:

- (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. 什么也没发生...我知道我正在保存并正确获取正确的Y轴值,但是当调用该JavaScript方法时,它将不会触发。

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! 为了进一步使事情复杂化,我继续进行了自定义IBAction,该操作访问了相同的 ySpot值,并使用完全相同的JavaScript方法将视图移动到位置200,并且效果很好!

What am I missing? 我想念什么? I don't see what is going on. 我不知道发生了什么。 Thanks 谢谢

edit: misread your question, 编辑:误读您的问题,

Perhaps the method you are calling to call that javascript is being called too fast? 也许您正在调用的调用javascript的方法调用速度太快? and not giving the UIWebview time to initialize and react? 并没有给UIWebview时间初始化和反应? if it works when you wait on the webview to come upand then call it, that is the only thing i could see. 如果它在您等待Webview出现然后调用它时起作用,那是我唯一能看到的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM