【问题标题】:Got Code To View Next Image, How to Change To View Previous?获得查看下一张图片的代码,如何更改为查看上一张?
【发布时间】:2010-11-13 13:58:43
【问题描述】:

我有下面的代码,这允许我单击一个按钮并切换到下一个图像,我如何制作这样当我单击另一个按钮时它会转到上一个图像?

- (IBAction)next {
static int index = 0;  // <-- here
    index++;
    // Set imageCount to as many images as are available
    int imageCount=31;
    if (index<=imageCount) {
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}

谢谢。

我现在有这个代码:

- (IBAction)prev {
      // <-- here
    index--;
    // Set imageCount to as many images as are available
    int imageCount=3;
    if (index<=0) {
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]];
    }
}

但它不起作用,它只是将图像视图更改为空白:'(

它还返回一个警告:未使用的变量 imageCount。

我现在已经实现了一个 ivar。

请帮忙谢谢

【问题讨论】:

    标签: iphone objective-c xcode ios4


    【解决方案1】:

    我认为您需要将索引设为全局变量或视图的 ivar:

    创建一个 Ivar:

    @interface MyView : UIView {
           int index;
    }
    @property (nonatomic) int index;
    
    @implementation MyView {
    @synthesize index;
    }
    

    【讨论】:

    • 我该怎么做,请您提供代码并告诉我放置它的位置,目前我只有一个带有上述代码的空白 xcode 项目,如果有帮助的话。
    • - (IBAction)prev { //
    【解决方案2】:

    我认为问题出在 if 语句,它检查索引 =0。 试试这个

    - (IBAction)prev {
    
          // <-- here
        index--;
        // Set imageCount to as many images as are available
        int imageCount = 30;
        if (index>=0) 
    
        {
    
            NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
            [picture setImage: [UIImage imageNamed: imageName]];
    
        }
    
    }
    

    注意:索引必须是 ivar 或全局变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      相关资源
      最近更新 更多