I’m playing around with a simple test app that just loops through 10 images and displays each one to the view. The next image will be displayed after the user clicks a button. The problem I am having is the view is never appearing (image or button). When I take out the button press clause, the code runs and the last image is displayed. continueRunningScript is initially YES.
@interface ViewController : UIViewController <UIAlertViewDelegate>
{
BOOL continueRunningScript;
UIButton *wrongButton;
UIImageView *imageView;
}
// ...
@end
@implementation ViewController
// ...
- (void) xxx {
for (int i = 0; i<10; i++) {
while (continueRunningScript == NO) {
// Stay here until button is pressed.
}
UIImage *testImg = ... // code to load the image at index i
imageView = [[UIImageView alloc] initWithImage: testImg];
[self.view addSubview:imageView];
wrongButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[wrongButton addTarget:self
action:@selector(incorrectResultButtonPress:)
forControlEvents:UIControlEventTouchUpInside];
[wrongButton setTitle:@"Wrong Result?" forState:UIControlStateNormal];
wrongButton.frame = CGRectMake(80.0, 310.0, 160.0, 40.0);
[self.view addSubview:wrongButton];
continueRunningScript = NO;
[testImg release];
}
[imageView release];
[wrongButton release];
}
// button press handling...
// UIAlertViewDelegate
- (void) alertView: (UIAlertView *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
// The user clicked one of the Yes/No buttons
if (buttonIndex == 0) // Yes
{
UIAlertView *thankyouAlert = [[UIAlertView alloc] initWithTitle:@"Thank You"
message:@"Thanks for the feedback!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[thankyouAlert show];
// Call performSelector delegate method of NSObject to call class method dismissAlertView
[self performSelector:@selector(dismissAlertView:) withObject:thankyouAlert afterDelay:2];
[thankyouAlert release];
[wrongButton removeFromSuperview];
continueRunningScript = YES;
}
}
// ...
@end
I’m making the assumption that you aren’t doing this on a background thread or anything. If you are, then you should be aware that UIKit is only designed to be used from the main thread.
This will not work the way you want it to. It will enter an infinite loop. The thread of execution will not progress past this point to process any input events, so that variable will never get a chance to be set to
YES.You have to remember that all of the user interface code will run after you exit from your method. If you take the pure out, then you’re looping through all the images that are added, then the user interface is updated. That’s why you only see the last image – the others were added too, but they are underneath the top-most (last) one.
If you want a new image to be shown every time the user taps a button, then you should have an instance variable that keeps track of which image is the current one, and write a method to update this variable and show the next image. In almost all circumstances, a busy loop like the above is wrong.
Also, you should be aware that you don’t have to add a new
UIImageViewevery time you want to display a new image. You can simply update theimageproperty for an existing image view to change which image it is displaying.