2012-12-31 43 views
7

我有一个NSMutableArray有9个对象。这些对象有很多属性,如questionNamequestionWhatRow更改NSMutableArray中所有对象的属性

我希望能够改变这些属性之一所有NSMutableArray对象。

这是我的NSMutableArray类。

-(id) init 
{ 
    self = [super init]; 

    if (self) 
    { 
     self.questionsArray = [[NSMutableArray alloc] init]; 

     Question *newQuestion = [[Question alloc] init]; 
     newQuestion.questionName = @"What is the name of the girl who has to fetch water for her family every day?"; 
     newQuestion.questionRowName = @"Question 1"; 
     newQuestion.questionAnswer = @"Nya"; 
     newQuestion.questionHint = @"Maybe if you read the book you would know"; 
     newQuestion.questionWhatRow = @"Nya"; 
     newQuestion.questionCompleteOrNot = @"No"; 
     newQuestion.pointForQuestion = 1; 
     [self.questionsArray addObject:newQuestion]; 

     newQuestion = [[Question alloc] init]; 
     newQuestion.questionName = @"What is Nya's sister's name?"; 
     newQuestion.questionRowName = @"Question 2"; 
     newQuestion.questionAnswer = @"Akeer"; 
     newQuestion.questionHint = @"Maybe if you read the book you would know"; 
     newQuestion.questionWhatRow = @"Nya"; 
     newQuestion.questionCompleteOrNot = @"No"; 
     newQuestion.pointForQuestion = 1; 
     [self.questionsArray addObject:newQuestion]; 

     newQuestion = [[Question alloc] init]; 
     newQuestion.questionName = @"What people is Nya's mother scared of when her father and sons go hunting?"; 
     newQuestion.questionRowName = @"Question 3"; 
     newQuestion.questionAnswer = @"Dinka"; 
     newQuestion.questionHint = @"Maybe if you read the book you would know"; 
     newQuestion.questionWhatRow = @"Nya"; 
     newQuestion.questionCompleteOrNot = @"No"; 
     newQuestion.pointForQuestion = 1; 
     [self.questionsArray addObject:newQuestion]; 

     newQuestion = [[Question alloc] init]; 
     newQuestion.questionName = @"What is Salva scared of when he is in the Akobo desert?"; 
     newQuestion.questionRowName = @"Question 4"; 
     newQuestion.questionAnswer = @"Lions"; 
     newQuestion.questionHint = @"He is scared of an animal because it ate his friend Marial"; 


     newQuestion = nil; 

    } 

    return self; 
} 

-(NSUInteger)count 
{ 
    return questionsArray.count; 
} 


- (Question *)questionAtIndex:(NSUInteger)index 
{ 
    return [questionsArray objectAtIndex:index]; 
} 

现在,我有一个名为ScoreViewController另一个类的,我希望能够给NSMutableArrayquestionsArray所有对象/问题的questionCompleteOrNot更改属性,以@"No"

我ScoreView有一个按钮复位,但我不知道该怎么把它改变questionsArray的所有questionCompleteOrNot属性@"No"

对不起,我试图说得很具体。

编辑

这是我的一个tableView的DetailView。

if ([[selectedQuestion questionCompleteOrNot] isEqualToString:@"No"]) 
    { 
     if ([[selectedQuestion questionAnswer] caseInsensitiveCompare:answerField.text] == NSOrderedSame) 
     { 
      // Show the correct label 
      [correctLabel setHidden:NO]; 
      correctLabel.text = @"Correct!"; 
      correctLabel.textColor = [UIColor greenColor]; 

     } 
     else 
     { 
      // Show the incorrect label 
      [correctLabel setHidden:NO]; 
      correctLabel.text = @"Incorrect"; 
      correctLabel.textColor = [UIColor redColor]; 
      [incorrectPlayer play]; 
      [[selectedQuestion questionCompleteOrNot] isEqualToString:@"Yes"]; 

     } 

     // Erase the text in the answerField 
     answerField.text = @""; 
    } 
    if ([[selectedQuestion questionCompleteOrNot] isEqualToString:@"Yes"]) 
    { 
     UIAlertView *error = [[UIAlertView alloc] 
           initWithTitle:@"Error" 
           message:@"You already answered this question. Please click the reset button on the score view to restart." 
           delegate:self 
           cancelButtonTitle:@"Okay!" 
           otherButtonTitles:nil]; 

     [error show]; 
     answerField.text = @""; 
    } 

    selectedQuestion.questionCompleteOrNot = @"Yes"; 
    correctLabel.text = @""; 
    NSLog(@"Selected question's questionCompleteOrNot is %@", [selectedQuestion questionCompleteOrNot]); 

回答

10

您可以使用NSArraymakeObjectsPerformSelector:withObject:,就像这样:

[questionsArray makeObjectsPerformSelector:@selector(setQuestionCompleteOrNot:) withObject:@"No"]; 
+0

我在ScoreView,QuestionList * QL = [[QuestionList的alloc]初始化]这样做; [ql.questionsArray makeObjectsPerformSelector:@selector(setQuestionCompleteOrNot :) withObject:@“No”] ;,但它仍然不起作用 – jakife

+0

定义“不起作用”。怎么了?你想做什么? QuestionList类如何与ScoreView类连接?我需要更多信息来确定什么是错的。我的答案应该适用于任何NSMutableArray,所以这听起来像你的问题是一个完全不同的问题。 –

+0

我有一个完全不同的观点,scoreView。我的另一个观点questionList有一个NSMutableArray。我不得不在本地获得QuestionList来访问数组。我想设置** all **所有对象的questionCompleteOrNot属性为No。似乎并没有这样做,因为当我忘记发布代码的另一个视图,我将在第二个视图中执行时该属性以查看它是否为并执行某些操作。在这里,我会在问题中发布它。 **编辑:只是更新它。** – jakife