some_id
some_id

Reputation: 29896

UIButton's image wont change correctly

I have a UIButton which I change its image twice.

Both changes work, but only if one is executed.

e.g. The first statement changes the image and it can be seen, but only if the second statement is not run. If I run both statements only the last image change can be seen.

It seems to skip the first image change.

These executions run in a timer and I have a slight delay on the second image change, hoping to avoid this problem.

Even if I change the delay, to what seems like enough time to see the image change, there is no visual change.

How can I avoid this issue and change the UIButtons image twice, making both changes visible?

EDIT ->

[A1 setImage:[UIImage imageNamed:fileString] forState:UIControlStateNormal];            
[self performSelector:@selector(changeImage:withString:) withObject:A1 withObject:fileString2];

I call these methods

 -(void) changeImage: (UIButton *) button withString: (NSString *) string
 {
   [button setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
 }

 -(void) startInvocation: (UIButton *) button withString: (NSString *) string
 {
     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                            [self methodSignatureForSelector:@selector(changeImage:withString:)]];
     [invocation setTarget:self];
     [invocation setSelector:@selector(changeImage:withString:)];
     [invocation setArgument:button atIndex:2];
     [invocation setArgument:string atIndex:3];
     [NSTimer scheduledTimerWithTimeInterval:0.15f invocation:invocation repeats:NO];
}

Thanks

Upvotes: 2

Views: 253

Answers (1)

VdesmedT
VdesmedT

Reputation: 9113

If you change the image twice in the same event loop, only the second image will be seen. Set the first image then start a NSTimer and program the event to set the second image. You should see the change.

-(void)switchImage {
    image.image = [UIImage imageNamed:@"dai00003.jpg"];    
}

-(IBAction) run {
    image.image = [UIImage imageNamed:@"dai00001.jpg"];
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(switchImage) userInfo:nil repeats:NO];
}

Upvotes: 1

Related Questions