某些进程正在运行时隐藏NSTextView

时间:2013-01-23 13:33:34

标签: objective-c cocoa nsthread nstextview nsprogressindicator

我的代码是这样的:

- (void)buttonClick
{
  [self.progressIndicator startAnimation:self];
  [self.textView setHidden:YES];  

  // some process

  [self.progressIndicator stopAnimation:self];
  [self.textView setHidden:NO];
}

问题是textView没有隐藏。 progressIndicator正常工作,也许是因为它在其他线程中运行。但是,当我试图在其他线程中隐藏textView或在后台执行时,它不起作用。

2 个答案:

答案 0 :(得分:0)

NSTextView嵌入NSScrollView您还需要隐藏NSScrollView

 - (void)buttonClick
{
  [self.progressIndicator startAnimation:self];
  [self.textView setHidden:YES]; 
  [scrollview setHidden:YES]; 

  // some process

  [self.progressIndicator stopAnimation:self];
  [self.textView setHidden:NO];
  [scrollview setHidden:NO];
}

答案 1 :(得分:0)

您可以延迟然后通过选择器调用,这将同步您的线程(但这可能不是最好的方法,您也可以使用OperationQueue和GCD)。但这符合你的目的:

我使用过NSTextField和NSTextView,因为你的问题和评论都包含两者。

-(void)someProcess:(id)sender{
    for (int i=0; i<100000; i++) {
        for (int j=0; j<9000; j++) {
            ;
        }
    }
    NSLog(@"someProcess ends");
}

-(void)startProgressIndicator{
    [self.progressIndicator startAnimation:self];
}
-(void)stopProgressIndicator{
    [self.progressIndicator stopAnimation:self];
}
-(void)hideScroll{
    [self.textField setHidden:YES];
    [self.scrollView setHidden:YES];
}
-(void)showScroll{
    [self.textField setHidden:NO];
    [self.scrollView setHidden:NO];
}


- (IBAction)button:(id)sender {
    //hide scrollview
    NSLog(@"hide sv");
    [self hideScroll];

    //start prog indi
    NSLog(@"run");
    [self performSelector:@selector(startProgressIndicator) withObject:self afterDelay:1];

    //some process
    [self someProcess:nil];


    //stop prog indi
    NSLog(@"stop");
    [self performSelector:@selector(stopProgressIndicator) withObject:self afterDelay:1];


    //show scrollview
    NSLog(@"show sv\n\n\n\n");
    [self performSelector:@selector(showScroll) withObject:self afterDelay:1];

}