2015-12-03 30 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

謝謝你們。現在我明白了。 –