私は、10枚の画像をループしてそれぞれの画像を表示する簡単なテストアプリで遊んでいます。ユーザーがボタンをクリックすると、次の画像が表示されます。私が抱えている問題は、ビューが表示されないことです(画像またはボタン)。ボタン押下句を取り出すと、コードが実行され、最後の画像が表示されます。 continueRunningScriptは最初はYESです。iPhoneに最後の画像しか表示されない
@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
これはループ内で同じ 'imageView'リファレンスを再割り当てするためです。ループを終了すると、常に最後のオブジェクトを指します。 – Joe
将来、投稿する前に、あなたのコードの書式をチェックしてください、私はあなたのためにそれを修正しました。 –
しかし、i = 0のループの最初の繰り返しでは何も表示されません。 –