我正在玩一個簡單的測試應用程序,它只是循環顯示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 2011-12-13 19:22:52
未來,在發佈之前,請檢查您的代碼格式,但我現在已經爲您修復了它。 – 2011-12-13 19:24:01
但是爲什麼在循環的第一次迭代中,當i = 0時什麼都不顯示給視圖? – 2011-12-13 19:41:19