2016-09-27 29 views
0

我還在學習過程中。我希望能夠點擊一個對象數組,並且該對象以可文本顯示。這是我得到多遠:點擊數組

我開始與位置0的對象。當一個按鈕被點擊標籤說一個。我再次點擊按鈕,標籤上寫着兩個,然後再次點擊三個。有人可以幫忙嗎? 感謝

- >編輯問:

我知道我需要算++莫名其妙,但我不知道如何正確使用它。如果我現在把它放在代碼的哪個位置,那麼標籤只會說兩個。是的,它應該是因爲它增加了一個,所以它的索引中的對象是「兩個」。它只在標籤中顯示「two」。那麼是否有一種方法可以使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]]; 
} 
+0

einArras = anArray – podoi17

+0

你並不需要一個循環。 – ozgur

回答

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 
+0

偉大的解決方案。完美的工作。我認爲你是一名高級程序員。我仍然在我的寶貝步驟,所以我從來沒有這樣的解決方案。任何意見如何可以與if語句一起工作? – podoi17

+0

self.counter =(self.counter + 1)%self.words.count。 能否真正快速解釋「%」的作用。 – podoi17

+0

這是模運算符... https://en.wikipedia.org/wiki/Modulo_operation – norders