0

我想在单击时隐藏按钮 loadImgButton 。此按钮将调用以下方法:

- (IBAction)produceImage:(id)sender
{

    [loadImgButton setHidden:YES];

    [image1 setImage:[UIImage imageNamed:@"Fanny2.JPG"]];
    [image2 setImage:[UIImage imageNamed:@"Fanny3.JPG"]];

    NSLog(@"i am here");

    for (int i = 0; i < 100000; i++) {
        for(int j = 0;j < 10000; j++) {

        }
    }
}

The problem is that the button is not hidden and the image is not set until the function is finish. What is the reason cause this situation and how to fix it? Thanks!
4

6 回答 6

1

UI 的更新发生在运行循环中,诸如setHidden简单地设置标志的方法,以便以后可以更新 UI。您可能想查看“ Is there a way to make drawRect work right now? ”以了解强制立即更新 UI 的方法。但是,您可能会更好地考虑其他方法来实现所需的结果。

于 2012-05-02T06:21:37.243 回答
0
- (IBAction)produceImage:(id)sender
{

     [loadImgButton setHidden:YES];

     // Call another function which download image with delay

    [self performSelector:@selector(image:) withObject:sender afterDelay:2];

}

- (IBAction)image:(id)sender
{
    [image1 setImage:[UIImage imageNamed:@"Fanny2.JPG"]];

    [image2 setImage:[UIImage imageNamed:@"Fanny3.JPG"]];

    NSLog(@"i am here");

    for (int i = 0; i < 100000; i++) {
        for(int j = 0;j < 10000; j++) {

        }
    }

}
于 2012-05-02T06:20:34.087 回答
0

您可能想要使用动画块,以便您可以在其完成时运行您的函数。您的代码的问题在于,我相信 setHidden 只是发送一个请求,该请求会在系统出现时执行。这就是为什么它首先运行您的功能。试试这个:

[UIView animateWithDuration:0.1 animations:^{

  [loadImgButton setHidden:YES];

} 
completion:^ (BOOL finished) {

 for (int i = 0; i < 100000; i++) {
    for(int j = 0;j < 10000; j++) {

    }
 }

}];
于 2012-05-02T06:22:13.500 回答
0

如果您的按钮没有隐藏,那么一旦您检查您的 nib 文件并检查您的按钮对象是否与文件连接?

于 2012-05-02T06:28:33.373 回答
0

创建 1 个新方法。

- (IBAction)produceImage:(id)sender

{

[loadImgButton setHidden:YES];

[self new];

} -(无效)新{

[image1 setImage:[UIImage imageNamed:@"Fanny2.JPG"]];
[image2 setImage:[UIImage imageNamed:@"Fanny3.JPG"]];

NSLog(@"i am here");

for (int i = 0; i < 100000; i++) {
    for(int j = 0;j < 10000; j++) {

    }
}

}

于 2012-05-02T06:42:36.900 回答
0

试试这个

-(IBAction)buttonPressed:(id)sender{

[(UIButton*)sender performSelectorInBackground:@selector(setHidden:) withObject:[NSNumber numberWithBool:YES]];
NSLog(@"heeere");

for (int i = 0; i < 100000; i++) {
    for(int j = 0;j < 10000; j++) {

    }
}

}

于 2012-05-02T08:44:23.950 回答