我有一个更新分页的功能,即计算总页数。
- (void) updatePagination{
if(!paginating){
NSLog(@"Pagination Started!");
paginating = YES;
totalPagesCount=0;
for (UIGestureRecognizer *gr in self.pageViewController.gestureRecognizers) {
[gr setEnabled:NO];
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self loadSpine:currentSpineIndex atPageIndex:currentPageInSpineIndex];
[[loadedEpub.spineArray objectAtIndex:0] setDelegate:self];
//This Line calls method of another class
[[loadedEpub.spineArray objectAtIndex:0] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];
[currentPageLabel setText:@"?/?"];
}
}
}
这是它调用的方法
- (void) loadChapterWithWindowSize:(CGRect)theWindowSize fontPercentSize:(int) theFontPercentSize{
fontPercentSize = theFontPercentSize;
windowSize = theWindowSize;
UIWebView* webView = [[UIWebView alloc] initWithFrame:windowSize];
[webView setDelegate:self];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath]];
[webView loadRequest:urlRequest];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[webView dealloc];
}
- (void) webViewDidFinishLoad:(UIWebView*)webView{
NSString *varMySheet = @"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 = [NSString stringWithFormat:@"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:@"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:@"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollWidth"] intValue];
pageCount = (int)((float)totalWidth/webView.bounds.size.width);
[webView dealloc];
[delegate chapterDidFinishLoad:self];
}
因此,我的 UI 不会被击中,但是当用户关闭或弹出视图应用程序时会崩溃。另一个增加和减小文本大小的按钮也称为相同的方法,因此如果它已经在运行并且用户单击调整它敲击的字体大小或有时会崩溃。
我的问题是:
- 是否可以在后台运行它,如果用户关闭模式,那么它应该停止而不是崩溃。
- 如果用户单击调整字体大小,那么它应该停止旧的并运行新的。
- 即使它的处理仍在继续,我可以使用计算的总页数吗?
我用了
[self performSelectorOnMainThread:@selector(LoadChapters) withObject:nil waitUntilDone:NO];
(有效,但有时会为 webview 报错)
和 NSOperation(不起作用)
调用那一行函数调用。