2016-09-28 53 views
1

我在Questions類中有一系列問題(NSArray *questions)。我在視圖上有一個標籤(questionLabel)和一個按鈕(nextButton)。當按下按鈕時,我希望標籤循環顯示一系列問題。我在Treehouse上做了類似的事情,但是他們使用了arc4random,這看起來不太好,因爲同一個問題可能會在更改前連續出現多次。我想無限循環,依次通過問題數組。我已將我的代碼複製到下面的按鈕。Objective-C使用按鈕循環訪問數組?

該應用程序運行但沒有任何反應,當我按下按鈕。我老實說花了兩週的時間搜索,學習等等,而且我沒有找到任何幫助我弄清楚這一點。很感謝任何形式的幫助。

此代碼是在ViewController.m文件:

- (IBAction)nextButton { 
    NSArray *questions = [[NSArray alloc] init]; 
    for (NSString *nextQuestion in questions) { 
     self.questionLabel.text = nextQuestion; 
    } 
} 
+0

當按鈕被點擊時,你爲什麼要做一個循環?你只是想將文本設置爲下一個問題,而不是所有的問題。 – rmaddy

回答

1

我不知道,我明白你的問題恰好不過,
你想看到的結果是這樣的?

#import "ViewController.h" 

@interface ViewController() 
@property (strong, nonatomic) IBOutlet UILabel *questionLabel; 
@property (nonatomic, strong) NSArray *questions; 
@end 

@implementation ViewController 
@synthesize questions; 
@synthesize questionLabel; 

- (IBAction)nextButton:(id)sender { 

    /* 
     this does alloc init and return array object containing no index data. 
    */ 
    // questions = [[NSArray alloc] init]; 

    for (NSString *nextQuestion in questions) { 
     self.questionLabel.text = nextQuestion; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    questions = [[NSArray alloc] initWithObjects:@"Question1", @"Question2", @"Question3", @"Question4", nil]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 


在這種情況下,你當然會得到零數組對象索引中的數據。

在代碼中,

- (IBAction)nextButton { 
    NSArray *questions = [[NSArray alloc] init]; 
    for (NSString *nextQuestion in questions) { 
     self.questionLabel.text = nextQuestion; 
    } 
} 

這確實分配/ init和返回的NSArray參考包含空索引數據,每當點擊了按鈕,就循環儘管空索引數據陣列對象。

==========================================
已編輯和更新
==========================================

// 
// ViewController.m 
// StackoverflowQuestion 
// 
// Created by Seoksoon Jang on 29/09/2016. 
// Copyright © 2016 Seoksoon Jang. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 
@property (strong, nonatomic) IBOutlet UILabel *questionLabel; 
@property (strong, nonatomic) IBOutlet UIButton *questionButton; 
@property (nonatomic, strong) NSArray *questions; 
@property (nonatomic, assign) BOOL stopRequest; 
@end 

@implementation ViewController 
@synthesize questions; 
@synthesize questionLabel; 
@synthesize questionButton; 
@synthesize stopRequest; 

#define LOOP_DELAY_TIME  0.1 

- (UIColor*)generateRandcomColor { 
    CGFloat hue = (arc4random() % 256/256.0); // 0.0 to 1.0 
    CGFloat saturation = (arc4random() % 128/256.0) + 0.5; // 0.5 to 1.0, away from white 
    CGFloat brightness = (arc4random() % 128/256.0) + 0.5; // 0.5 to 1.0, away from black 
    return randomColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; 
} 

- (IBAction) stopButton:(id)sender { 
    stopRequest = !stopRequest; 
} 

- (IBAction) nextButton:(id)sender { 

    self.questionButton.enabled = NO; 

    for (NSString *nextQuestion in questions) { 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.questionLabel.text = nextQuestion; 
      self.questionLabel.backgroundColor = [self generateRandcomColor]; 
     }); 

     [NSThread sleepForTimeInterval:LOOP_DELAY_TIME]; 
    } 

    if (stopRequest) { 
     stopRequest = !stopRequest; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.questionButton.enabled = YES; 
     }); 

     return ; 

    } else { 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      [self performSelector:@selector(nextButton:) withObject:nil]; 
     }); 
    } 
} 

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

    questions = [[NSArray alloc] initWithObjects:@"Why is Kimchi the best food in the world", @"Why is Bacon healthy?", @"Why is Pizza delicious?", @"Why is Tuna nice?", nil];  
} 

- (void) didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

這是如此簡單的UI界面和基於此源代碼的結果截圖。


*故事板UI示例屏幕截圖*
enter image description here




*結果*
enter image description here

================= =========================
編輯和更新2
==========================================

#import "ViewController.h" 

@interface ViewController() 
@property (strong, nonatomic) IBOutlet UILabel *questionLabel; 
@property (strong, nonatomic) IBOutlet UIButton *questionButton; 
@property (strong, nonatomic) NSArray *questions; 
@property (assign, nonatomic) NSUInteger count; 
@end 

@implementation ViewController 
@synthesize questions; 
@synthesize questionLabel; 
@synthesize questionButton; 
@synthesize count; 

- (IBAction)nextButton:(id)sender { 

    if (count >= [questions count]) { 
     count = 0; 
    } 

    self.questionLabel.text = [questions objectAtIndex:count++]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    questions = [[NSArray alloc] initWithObjects:@"Question1", @"Question2", @"Question3", @"Question4", nil]; 

    count = 0; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

enter image description here

抱歉錯誤地提出您的問題。
可能吧,它不是你想要的。
我只是爲了好玩。所以,不要覺得沉重。

+0

非常感謝花時間把這些放在一起。所以我完全按照您的建議使用代碼,但是當按下按鈕時,它只是將標籤帶到數組中的最後一個對象。當我按下按鈕時,我希望標籤無限次地遍歷數組。你認爲你可以幫助我嗎?再次感謝! –

+0

好的,在編輯過的源代碼中,我儘量瞭解你的問題。無論如何,這是正確的請求後,你的需求? – boraseoksoon

+0

再次感謝您抽出寶貴時間寫下這篇文章,但唯一不同的是我只需點擊一下 - 下一個問題,單擊 - 下一個問題,單擊 - 下一個問題等等是否有意義?因此,用戶單擊該按鈕可逐個獲取數組中的下一個問題。但就像我說過的,我可以使用arc4random來做到這一點,但同一個問題連續出現多次。 –

0

我要你做這樣的接口這

在viewDidLoad中聲明

int i,j; 
NSArray questions; 

現在,所有對象添加到array

j=0; 
i=j; 
questions = [[NSArray alloc]initwithObjects : "YOUR QUESTION","YOUR QUESTION"..........,nil]; 

現在按鈕操作方法

-(IBAction)nextButton : (sender) 
{ 
    for (i = j; i < [questions count]; i++) 
    { 
     self.questionLabel.text = [quesions objecAtIndex:i]; 
     j = i; 
     break; 
    } 
} 

希望這個事情的作品

+0

非常感謝您將它們放在一起。我也按照您的建議嘗試了這一點,但按鈕只是將標籤更改爲數組中的一個對象,而不是遍歷整個數組。是否有任何修復?謝謝! –

+0

主要是你想要什麼?根據我的理解,您每次點擊按鈕時都想更改問題。因此,根據我的代碼工作正常 –

+0

對不起,我的意思是,當我點擊按鈕,它只是停在數組中的一個對象上,然後當我再次單擊時,nothings發生,所以它卡在一個對象上。我希望它一次只能移動一個點擊一個問題。 –