我不知道,我明白你的問題恰好不過,
你想看到的結果是這樣的?
#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](https://i.stack.imgur.com/iKQpj.png)
*結果*
![enter image description here](https://i.stack.imgur.com/vBbUU.gif)
================= =========================
編輯和更新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](https://i.stack.imgur.com/17SUY.gif)
抱歉錯誤地提出您的問題。
可能吧,它不是你想要的。
我只是爲了好玩。所以,不要覺得沉重。
當按鈕被點擊時,你爲什麼要做一個循環?你只是想將文本設置爲下一個問題,而不是所有的問題。 – rmaddy