I've read most of a couple of good books over the past couple years on iOS programming and went out of practice for a bit but coming back into now and there's a simple fundamental thing I can't wrap my head around when it comes to Objective-C and it's how events happen. Specifically how do I have a series of events happen rather than not all at one time. Here's a basic example I made to show you what I mean - I have a feeling I'm just approaching this all wrong and so my "work-around" is a terrible idea or whatever - I don't know.
Let's say I have a button called "goButton" and a label called "label". When I click the button I want the label to wait 2 seconds and read "Red", and then wait 2 more seconds and read "Yellow" and then 2 more seconds and read "Green". So when I looked up how to use "afterDelay" and did it the way I thought it would be done, it basically just waited 2 seconds and then changed to "Green" - which leads me to believe that all 3 of those delays happened at one time and we just arrived at the end - "Green". So I changed it to this to "work-around" but I feel like there must be a better way to approach this and that I just don't really understand how things happen when going through this code. In my mind if you have a line that has a delay, it should have to wait that delay out before going to the next line, but maybe that would be true if these were separate actions and after the delay you have to call the next action too? But that seems even messier. Here's my work-around that works but just doesn't seem right nor very flexible - obviously the work-around is me adding the extra delays to the successive events (so the delays are 2, 4, and then 6):
Thanks for any help with this. I think I'm missing a fundamental part of Objective C and it probably turns this into a really stupid question but I really appreciate even making me feel dumb at this point. thanks again
Let's say I have a button called "goButton" and a label called "label". When I click the button I want the label to wait 2 seconds and read "Red", and then wait 2 more seconds and read "Yellow" and then 2 more seconds and read "Green". So when I looked up how to use "afterDelay" and did it the way I thought it would be done, it basically just waited 2 seconds and then changed to "Green" - which leads me to believe that all 3 of those delays happened at one time and we just arrived at the end - "Green". So I changed it to this to "work-around" but I feel like there must be a better way to approach this and that I just don't really understand how things happen when going through this code. In my mind if you have a line that has a delay, it should have to wait that delay out before going to the next line, but maybe that would be true if these were separate actions and after the delay you have to call the next action too? But that seems even messier. Here's my work-around that works but just doesn't seem right nor very flexible - obviously the work-around is me adding the extra delays to the successive events (so the delays are 2, 4, and then 6):
Code:
-(IBAction)goButton:(id)sender {
[self performSelector:@selector(red) withObject:nil afterDelay:2];
[self performSelector:@selector(yellow) withObject:nil afterDelay:4];
[self performSelector:@selector(green) withObject:nil afterDelay:6];
}
-(void)red {
label.text = @"Red";
}
-(void)yellow {
label.text = @"Yellow";
}
-(void)green {
label.text = @"Green";
}
Thanks for any help with this. I think I'm missing a fundamental part of Objective C and it probably turns this into a really stupid question but I really appreciate even making me feel dumb at this point. thanks again