Possible Duplicate:
iPhone App Dev – Loading View From View Controller
I have a root view controller with a tool bar which has a button. When the root view controller loads I want it to load a subview underneath the toolbar:
//assigns JSON to question objects
-(void) setQuestions {
questionArray = [[NSMutableArray alloc] init];
for (NSDictionary *q in self.questions) {
/* Create our Question object and populate it */
QuestionViewController *question = [[QuestionViewController alloc]init];
[question setQuestionId:[q objectForKey:@"questionId"] withTitle:[q objectForKey:@"question"] number:[q objectForKey:@"questionNumber"] section:[q objectForKey:@"sectionId"]];
/* Add it to our question (mutable) array */
[questionArray addObject:question];
[question release];
}
}
-(void) startQuestionnaire {
currQ = [questionArray objectAtIndex:0];
[self.view insertSubview:currQ.view atIndex:0];
[currQ release];
}
I use the startQuestionnaire to load the viewcontroller from questionArray which contains a load of QuestionViewController objects…When I click on the slider in the view that is loaded the program crashes…Do I have have to hand control over to the subview or something?The program doesnt crash when I emove the code within startQuestionnaire
While it’s tough to say without seeing the rest of your code, it looks like your problem is somewhere in the way you’re instantiating / referencing objects.
If I were you, I’d approach this slightly differently in the interest of simplifying things.
First off, try to use @property and @synthesize, and get used to referencing ivars with ‘self’. Retain counts & memory management issues are often the cause of crashes, and you can clean things up quite a bit by being more formal with your ivars. Here’s some reading: http://www.optictheory.com/iphone-dev/2010/02/objective-c-primer-part-3-property-and-synthesize/
Second, I would refrain from creating your
QuestionViewControlleruntil you’re ready to add it to your root ViewController (ie. insidestartQuestionnaire). You can pass your JSON data into it with a custom init function. This way, you can ensure that there aren’t other QuestionViewController instances lying around that might be causing complications, and taking up memory when they’re not being used.As for the slider issue itself, it’s tough to say without seeing the actual error, but hopefully this helps a bit.
Best of luck