嗨,我正在開發一個測驗應用程序。我希望標籤在運行應用程序時出現在數組中的問題中。我創建了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