iPhone MultiView App问题

时间:2011-07-14 23:04:19

标签: iphone objective-c xcode

  

可能重复:
  iPhone App Dev - Loading View From View Controller

我有一个带有工具栏的根视图控制器,它有一个按钮。当根视图控制器加载时,我希望它在工具栏下面加载子视图:

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

我使用startQuestionnaire从questionArray加载viewcontroller,其中包含一堆QuestionViewController对象...当我点击加载的视图中的滑块时程序崩溃...我是否必须手动控制到子视图或其他什么?当我在startQuestionnaire

中移动代码时,程序不会崩溃

1 个答案:

答案 0 :(得分:0)

虽然很难说没有看到你的其余代码,但看起来你的问题就像你实例化/引用对象一样。

如果我是你,为了简化事情,我会稍微改变一下。

首先,尝试使用@property和@synthesize,并习惯用'self'引用ivars。保留计数&内存管理问题通常是崩溃的原因,你可以通过更加正式的ivars清理一下。这里有一些阅读:http://www.optictheory.com/iphone-dev/2010/02/objective-c-primer-part-3-property-and-synthesize/

其次,在您准备将其添加到根ViewController(即QuestionViewController内)之前,我不会创建您的startQuestionnaire。您可以使用自定义init函数将JSON数据传递给它。这样,您可以确保没有其他可能导致复杂化的QuestionViewController实例,并在不使用时占用内存。

至于滑块问题本身,很难说没有看到实际的错误,但希望这有点帮助。

祝你好运