只在iPhone上显示最后一张图像

问题描述:

我正在玩一个简单的测试应用程序,它只是循环显示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 
+0

这是因为您正在循环中重新分配相同的`imageView`引用。当你完成循环时,它总是指向最后一个对象。 – Joe 2011-12-13 19:22:52

+0

未来,在发布之前,请检查您的代码格式,但我现在已经为您修复了它。 – 2011-12-13 19:24:01

+0

但是为什么在循环的第一次迭代中,当i = 0时什么都不显示给视图? – 2011-12-13 19:41:19

我假设你没有在后台线程或任何东西上执行此操作。如果你是,那么你应该知道,UIKit只能用于主线程。

while (continueRunningScript == NO) { 
    // Stay here until button is pressed. 
} 

这不会按照您希望的方式工作。它将进入一个无限循环。执行线程不会超过此点来处理任何输入事件,因此变量将永远不会有机会设置为YES

您必须记住,退出您的方法后,所有用户界面代码都将运行。如果你把纯粹的,然后你循环所有的图像被添加,然后用户界面被更新。这就是为什么你只看到最后一张图像 - 其他图像也被添加了,但是它们在最上面(最后一张)下面。

如果你想每次用户点击一个按钮时显示一个新的图像,那么你应该有一个实例变量,跟踪哪个图像是当前的图像,并编写一个方法来更新这个变量并显示下一张照片。在几乎所有情况下,像上面这样的繁忙循环都是错误的。

此外,您应该意识到,每次想要显示新图像时都不必添加新的UIImageView。您只需更新现有图像视图的image属性即可更改显示的图像。

我不认为你需要释放imageView或wrongButton。只要控制器存在,他们可能希望被保留。

除了这个猜测,我建议我们需要更多的信息。

方法'xxx'在主线程上运行吗?如果是这样,罪魁祸首是:

while (continueRunningScript == NO) { 
     // Stay here until button is pressed. 
    } 

的iOS使用事件驱动的架构,这是非常不好的做法,阻止主线程像你在这里做什么。你的应用程序将无限期地在这里循环,并停止更新UI,并且不会响应任何按钮按下或其他事件。这就是为什么你的视图,图像和按钮没有出现。

您需要考虑如何修改您的应用以适应事件驱动架构。

当您返回到主UI运行循环时,UIKit只绘制东西。您的代码在第一张图像之后和下一张之前不会返回,因此第一张图像从不显示。

只在主UI运行循环中检测/分派事件。由于您的代码永远不会从第一个按钮处理程序返回,因此不会检测到后续按钮事件。