Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 单击时循环浏览图像的UI按钮_Iphone - Fatal编程技术网

Iphone 单击时循环浏览图像的UI按钮

Iphone 单击时循环浏览图像的UI按钮,iphone,Iphone,我有三个图像,我想循环通过时,点击一个按钮 我该怎么做呢 到目前为止,这就是我所拥有的,但它并没有真正起作用 //the images NSString* imageNames[] = {"MyFirstImage", "AnotherImage", whatever else}; int currentImageIndex = 0; 及 想法 任何帮助都将不胜感激!谢谢 您需要将UIButton子类化,并添加一个维护状态的新属性。您还需要三个属性来维护三个新状态的图像。然后在“drawRec

我有三个图像,我想循环通过时,点击一个按钮

我该怎么做呢

到目前为止,这就是我所拥有的,但它并没有真正起作用

//the images
NSString* imageNames[] = {"MyFirstImage", "AnotherImage", whatever else};
int currentImageIndex = 0;

想法


任何帮助都将不胜感激!谢谢

您需要将UIButton子类化,并添加一个维护状态的新属性。您还需要三个属性来维护三个新状态的图像。然后在“drawRect:”方法中,根据您的状态交换标准按钮图像,然后调用[super drawRect:]方法。

Steven建议将UIButton子类化。一旦你得到了更多的经验与目标C,你应该考虑他的方法,但我可以告诉你的代码,你张贴,你是新的目标C,所以你可能需要学会使用基本的基础类首先。p> 代码无法工作的一个原因是您试图将C字符串文本传递给pathForResource:,这需要NSString对象。NSStrings是Objective C中的对象,而不是C中的字符指针。您可以使用文字语法@“前面带at的引号”构造NSStrings对象

这里是使用ValueC基础类而不是C数据类型实现您试图编写的算法的代码:

// YourController.h
@interface YourController :  UIViewController {
    NSArray *imageNames;
    NSInteger currentImageIndex;
    UIButton *yourButton;
}
@property (nonatomic, retain) NSArray *imageNames;
@property (nonatomic, retain) IBOutlet UIButton *yourButton; // Presumably connected in IB
- (IBAction)change;
@end

这似乎是一种非常复杂的实现方式。这是唯一的办法吗?
// YourController.h
@interface YourController :  UIViewController {
    NSArray *imageNames;
    NSInteger currentImageIndex;
    UIButton *yourButton;
}
@property (nonatomic, retain) NSArray *imageNames;
@property (nonatomic, retain) IBOutlet UIButton *yourButton; // Presumably connected in IB
- (IBAction)change;
@end
// YourController.m
#import "YourController.h"
@implementation YourController
@synthesize imageNames, yourButton;

- (void)dealloc {
    self.imageNames = nil;
    self.yourButton = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    self.imageNames = [NSArray arrayWithObjects:@"MyFirstImage", @"AnotherImage", nil];
    currentImageIndex = 0;
    [super viewDidLoad];
}

- (IBAction)change {
    UIImage* imageToShow = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[imageNames objectAtIndex:currentImageIndex] ofType:@"png"];
    currentImageIndex++;
    if (currentImageIndex >= imageNames.count) {
        currentImageIndex = 0;
    }
    [yourButton setImage:imageToShow forState:UIControlStateNormal];
}

@end