0

I want to hide the button loadImgButton when I click it. This button will call the method below:

- (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!
itenyh
  • 1,921
  • 2
  • 23
  • 38

6 Answers6

1

Updates to the UI happen in the run loop, methods such as setHidden are simply setting flags so that the UI can be updated later. You might want to look at "Is there a way to make drawRect work right NOW?" for ways to force the UI to be updated immediately. However, you might be better thinking about other ways to achieve the result you require.

Community
  • 1
  • 1
mttrb
  • 8,297
  • 3
  • 35
  • 57
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++) {

        }
    }

}
mttrb
  • 8,297
  • 3
  • 35
  • 57
Mangesh
  • 2,257
  • 4
  • 24
  • 51
  • this really works! what does this line "[self performSelector:@selector(image:) withObject:sender afterDelay:2]; " mean? and how it works? – itenyh May 02 '12 at 07:17
  • This call a function with delay. We hide the button and then call code for set image. – Mangesh May 02 '12 at 09:08
0

You probably want to use an animation block so that you can run your function on its completion. The problem with your code is that I believe that setHidden just sends a request that gets executed when the system comes around to it. That's why it is running your function first. Try this:

[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++) {

    }
 }

}];
Jamie
  • 5,090
  • 28
  • 26
0

If your button is not hidden that once you check your nib file and check out your button object is connected with file onwer or not?

vishiphone
  • 750
  • 7
  • 14
0

Create 1 new method.

- (IBAction)produceImage:(id)sender

{

[loadImgButton setHidden:YES];

[self new];

} -(void)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++) {

    }
}

}

k.shree
  • 119
  • 1
  • 2
  • 12
0

Try this

-(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++) {

    }
}

}

Mejdi Lassidi
  • 999
  • 10
  • 22