2010-06-30 75 views
0

解析器(在单独的线程上)完成后,我一直试图添加一个按钮。我知道你不能在主线程以外的任何线程上与UI元素进行交互。解析器(在单独的线程上)完成后添加UIButton

我不想用一个定时器,或while语句...所以我的问题是

一旦你有什么建议我做的按钮添加到视图解析器做了什么?我不希望它之前添加,因为用户将获得一个空白表。我也不想在完成时重新加载表格,因为这会给我带来其他问题。

主线程上的performSelector对我来说似乎不起作用..?我有点迷失在这里...

有什么建议吗?


这里就是我踢给另一个线程来启动解析器(在AppDelegate中)

// begin background downloads 
[NSThread detachNewThreadSelector:@selector(parseNewData) toTarget:self withObject:nil]; 

我parseNewData功能(在AppDelegate中)

-(void)parseNewData { 

    //start network activity spinner and release controller when done 
    RootViewController * root = [[RootViewController alloc] init]; 
    [root downloadIcon]; 
    [root release]; 

    //create pool to avoid memory leak 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 


    // get the XML path and start parsing 
    NSURL *pathURL = [NSURL URLWithString:@"http://www.mysite.com/file.xml"]; 
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:pathURL]; 
    [parser setDelegate:self]; 
    [parser parse]; 

    //drain pool 
    [pool drain]; 
    [pool release]; 

} 

分析器去把(在AppDelegate中)

- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 

    // parser is finished, we can now kill the network activity icon on root view controller 
    RootViewController * root = [[RootViewController alloc] init]; 
    [root killDownloadIcon]; 
    [root performSelectorOnMainThread:@selector(unhideShowtimesButton) withObject:nil waitUntilDone:NO]; 
    [root release]; 

} 

上(在RootViewController的)

-(void)unhideShowtimesButton { 
    showtimesButton.hidden = FALSE; 
} 

我做它对我unhideShowtimesButton(通过断点证实),但它只是完全无视隐藏=假我unhideShowtimesButton。

+0

通知如何? – ennuikiller 2010-06-30 19:07:28

+0

我添加了一些代码,你看到什么错误?谢谢! – Louie 2010-06-30 19:36:34

回答

1

您正在每个函数中创建一个新的RootViewController实例。这看起来不对。您应该在整个代码中只使用一个实例。我的直觉是,由于RootViewController实例不同,所以您试图隐藏的showTimesButton实例与正在显示的实例不同。

+0

这是有道理的!让我给出一个镜头 – Louie 2010-06-30 19:43:15

+0

我修正了这些实例,但仍然忽略了“unhide”声明。 :( 还是)感谢你的建议 – Louie 2010-06-30 22:25:55

相关问题