如何在Objective C中运行,重启和终止线程?

时间:2012-02-23 06:58:17

标签: iphone ipad nsthread background-thread

我有一个更新分页的功能,即计算总页数。

   - (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];

}

因为这个我的用户界面没有被触及,但是当用户解雇或弹出视图应用程序时会崩溃。另外一个增加和减小文本大小的按钮也称为相同的方法,因此如果它已经运行并且用户点击它会调整它所打击的字体或者有时会崩溃。

我的问题是:

  • 是否可以在后台运行它,如果用户关闭模态 然后它应该停止而不是崩溃。
  • 如果用户点击调整字体大小,则应该停止旧版并运行新版本。
  • 我可以利用计算的总页数,即使其处理仍在继续。

我用过

[self performSelectorOnMainThread:@selector(LoadChapters) withObject:nil waitUntilDone:NO];(但有时会为webview提供错误)

和NSOperation(不起作用)

调用该单行函数调用。

0 个答案:

没有答案