【问题标题】:How to View Next Image in An Array Using Index/Integer - iPhone Dev如何使用索引/整数查看数组中的下一个图像 - iPhone Dev
【发布时间】:2011-03-26 09:12:22
【问题描述】:

对,

我有一个如下所示的数组:

-(void) viewDidLoad;
{
    imageArray = [[NSArray arrayWithObjects: 
                [UIImage imageNamed:@"1.png"], 
                  [UIImage imageNamed:@"2.png"], 
                  nil] retain];
}

而且我有一个 IBAction,当单击按钮时触发如下:

-(IBAction)next {

    UIImage *img = [imageArray objectAtIndex:0];
    [imageView setImage:img];   

}

问题在于,当单击按钮时,它只会选择索引 0 处的对象(显然!)。我想知道的是如何在 .h 文件中使用整数定义索引,并在 IBAction 中使用它,这样每当我单击下一个按钮时,它将转到数组中的下一个图像而不是索引 0每次?

【问题讨论】:

    标签: iphone cocoa-touch xcode


    【解决方案1】:

    试试这个:

    int current_image_index = 0;
    
    -(IBAction)next {
    
        if (current_image_index + 1 == [imageArray count])
              current_image_index = 0;
        UIImage *img = [imageArray objectAtIndex:current_image_index];
        [imageView setImage:img]; 
        current_image_index++;
    
    
    
    }
    

    【讨论】:

    • 该代码构建良好,比我的要好,但无论我单击下一步多少次,它仍然只显示数组中的第一个图像。
    • @Ian : @ennukiller 代码看起来我工作了这么几个查询,你在哪里声明了“current_image_index”?并在代码中的哪个位置将默认值 (0) 分配给“current_image_index”,
    • @Jhaliya 我还在习惯这些东西,所以我可能没有正确定义东西。 current_image_index 我在我的 .h 文件中使用:NSNumber *current_image_index 定义。然后我有 int current_image_index = 0;在任何代码块之外的 .m 文件中。这样,当我再次单击按钮以获取数组中的下一个图像时,应用程序会崩溃
    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多