2015-12-03 34 views
0

嗨,我正在开发一个测验应用程序。我希望标签在运行应用程序时出现在数组中的问题中。我创建了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 
+0

尝试移动代码'self.questions = ...'到'viewDidLoad'; – KudoCC

回答

0

您需要的问题初始化移动到awakeFromNib()viewDidLoad()因为故事板不使用initWithNibName

-1

尝试这样的事:

 #import "ViewController.h" 
    @interface ViewController() 
    @property(nonatomic,assign)int currentQuestionIndex; 
    @property(nonatomic,copy)NSArray *questions; 
    @property (weak, nonatomic) IBOutlet UILabel *questionLabel; 
    @end 

    @implementation ViewController 



    - (IBAction)showQuestion:(id)sender 
    { 
     [self showQuestion]; 
     } 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
self.questions [email protected][@"Who is the president of US?",@"What is 7 + 7?",@"What is the capital of Vermont?"]; 

self.currentQuestionIndex= 0; 
[self showQuestion]; 
     } 

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

-(void)showQuestion{ 
// Step to the next question 


     // 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; 

self.currentQuestionIndex++; 

     // Am I pas the last question? 
     if (self.currentQuestionIndex == [self.questions count]) { 

      // Go back to the first question 
      self.currentQuestionIndex = 0; 
     } 


} 

    @end 
+0

谢谢你们。现在我明白了。 –