单击一个数组

时间:2016-09-27 19:01:50

标签: objective-c arrays loops button

我还在学习中。我想能够点击一个对象数组,这个对象以可纹理的方式显示。这是我有多远:

我从第0位的对象开始。当点击一个按钮时,标签会显示一个。我再次点击按钮,标签上写着两个,然后再点击它就说三个。有人可以帮忙吗? 感谢

- >编辑问题:

我知道我需要计算++,但我不确定如何正确使用它。如果我现在把它放在代码中的位置,标签只会说两个。是的,它应该是因为它增加了一个,所以它的索引处的对象是"两个"。它只显示"两个"在标签上。那么有没有办法让它与if语句一起使用?

NSString *word = {@"one,two,three"};
NSArray *anArray = [word componentsSeparatedByString:@","];
 int count = anArray.count;
 count = 0;
count++;

if (count == 0){
_labelText.text = [NSString stringWithFormat:@"%@" , [anArray  objectAtIndex:0]];
}

else if(count == 1){
_labelText.text = [NSString stringWithFormat:@"%@", [anArray objectAtIndex:1]];
}

else if (count ==2){
_labelText.text = [NSString stringWithFormat:@"%@", [anArray objectAtIndex:2]];
}

1 个答案:

答案 0 :(得分:1)

试试这个:

@interface ViewController ()

@property (nonatomic, weak) IBOutlet UILabel *labelText;
@property (nonatomic, strong) NSArray *words;
@property (nonatomic, readwrite) NSInteger counter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.words = @[@"one", @"two", @"three"];
    self.counter = 0;

    [self updateUI];
}

- (IBAction)nextButton:(id)sender {
    self.counter = (self.counter + 1) % self.words.count;
    [self updateUI];
}

- (void)updateUI {
    self.labelText.text = self.words[self.counter];
}

@end