嗨,我正在开发一个测验应用程序。我希望标签在运行应用程序时出现在数组中的问题中。我创建了UIlable。我用init方法不起作用。我的代码如下如何使用数组内容初始化ios中的标签?
#import "ViewController.h"
@interface ViewController()
@property(nonatomic,assign)int currentQuestionIndex;
@property(nonatomic,copy)NSArray *questions;
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@end
@implementation ViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];
if (self) {
//fill two array
self.questions [email protected][@"Who is the president of US?",@"What is 7 + 7?",@"What is the capital of Vermont?"];
}
//return the address of new object
return self;
}
- (IBAction)showQuestion:(id)sender
{
// Step to the next question
self.currentQuestionIndex++;
// Am I pas the last question?
if (self.currentQuestionIndex == [self.questions count]) {
// Go back to the first question
self.currentQuestionIndex = 0;
}
// Get the string at the index in the questions array
NSString *question = self.questions[self.currentQuestionIndex];
// Display the string in the question label
self.questionLabel.text = question;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
尝试移动代码'self.questions = ...'到'viewDidLoad'; – KudoCC