2011-07-19 114 views
0

忍受我在这一个。iPhone应用程序错误问题

我有一个iphone应用程序。这是一个问卷调查应用程序。有几种类型的问题,一些有滑块,一些有文本输入等。我已经为每种类型的问题开发了一个视图控制器。

问题控制器的两个示例类型是:TextInputQuestionViewController和SliderQuestionViewController。

我有一个名为QuestionnaireViewController的rootViewcontroller。这是定义如下:

#import <UIKit/UIKit.h> 
#import "JSONKit.h"; 
#import "dbConnector.h" 
#import "SliderQuestionViewController.h"; 
#import "TextInputQuestionViewController.h"; 
#import "MainMenuProtocol.h"; 

@interface QuestionnaireViewController : UIViewController { 
    NSDictionary* questions; 
    NSMutableArray* questionArray; 
    NSMutableArray* answerArray; 
    dbConnector* db; 
    SliderQuestionViewController* currQ; //need to create a generic var 
    TextInputQuestionViewController* currQ; 
    NSInteger currQNum; 
    NSString* qaTitle; 
    NSString* secId; 
    id<MainMenuProtocol>delegate; 
} 

@property(nonatomic, retain) NSDictionary* questions; 
@property(nonatomic, retain) NSMutableArray* questionArray; 
@property(nonatomic, retain) NSMutableArray* answerArray; 
@property(nonatomic, retain) dbConnector* db; 
@property(nonatomic, retain) SliderQuestionViewController* currQ; 
@property(nonatomic, retain) TextInputQuestionViewController* currTI; 
@property(nonatomic) NSInteger currQNum; 
@property(nonatomic, retain) NSString* qaTitle; 
@property(nonatomic, retain) NSString* secId; 
@property(nonatomic, retain) id <MainMenuProtocol> delegate; 

-(void) setQuestions; 
-(void) startQuestion:(NSInteger)index isLast:(BOOL)last; 
-(void) loadQuestions; 
-(void) initialise; 
-(void) finishQuestionnaire:(id)sender; 
-(void) switchViews:(id)sender; 


@end 



#import "QuestionnaireViewController.h" 
#import "dbConnector.h" 
#import "ASIHTTPRequest.h" 
#import "JSONKit.h"; 
#import "Answer.h"; 


@implementation QuestionnaireViewController 
@synthesize questions, questionArray, db, currQ, currQNum, answerArray, qaTitle, secId, delegate; 

-(void)viewDidLoad{ 
    [self initialise]; 
    answerArray = [[NSMutableArray alloc]init]; 
    [super viewDidLoad]; 
    self.title = qaTitle; //set to whatever section is 
} 

-(void) initialise { 
    currQNum = 0; 
    [self loadQuestions]; 
    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(switchViews:)];   
    self.navigationItem.rightBarButtonItem = anotherButton; 
} 

-(void) loadQuestions { 
    db = [[dbConnector alloc]init]; 
    //code to initialise view 
    [db getQuestions:secId from:@"http://dev.speechlink.co.uk/David/get_questions.php" respondToDelegate:self]; 
} 

//called when questions finished loading 
//stores dictionary of questions 
- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    NSData *responseData = [request responseData]; 
    NSString *json = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSDictionary *qs = [json objectFromJSONString]; 
    self.questions = qs; 
    [json release]; 
    [qs release]; 
    [self setQuestions]; 
} 


//assigns JSON to question objects 
-(void) setQuestions { 
    questionArray = [[NSMutableArray alloc] init]; 
    for (NSDictionary *q in self.questions) {    
     /* Create Question object and populate it */ 
     id question; 
     if([[q objectForKey:@"type"] isEqualToString:@"Slider"]){ 
      question = [[SliderQuestionViewController alloc]init]; 
      //set min max values 
     }else if([[q objectForKey:@"type"] isEqualToString:@"Option"]){ 

     }else if([[q objectForKey:@"type"] isEqualToString:@"TextInput"]){ 
      question = [[TextInputQuestionViewController alloc]init]; 
     }else if([[q objectForKey:@"type"] isEqualToString:@"ImagePicker"]){ 

     }else{ 
      //comments 

     } 
     //if else to create appropriate view controller - NEED to identify question type 

     [question setQuestionId:[q objectForKey:@"questionId"] withTitle:[q objectForKey:@"question"] number:[q objectForKey:@"questionNumber"] section:[q objectForKey:@"sectionId"] questionType: [q objectForKey:@"type"]]; 
     /* Add it to question (mutable) array */ 
     [questionArray addObject:question]; 
     [question release]; 
    } 
} 

-(void) startQuestion:(NSInteger)index isLast:(BOOL)last{ 
    //currQ = [[QuestionViewController alloc]init]; 
    currQ = [questionArray objectAtIndex:index]; 
    //push currQ onto navigationcontroller stack 
    [self.navigationController pushViewController:currQ animated:YES]; 
    [currQ addButton:self isLast: last]; 
} 

//pushes new view onto navigation controller stack 
-(void) switchViews:(id)sender{ 
    Answer* ans = currQ.question.answer; 
    ans.questionId = currQ.question.qId; 
    ans.entryId = @"1";//temporary; 
    if(currQNum < [questionArray count] - 1){  
     if(currQNum > 0){   
      //if else for different input types 
      NSString* qt = currQ.question.qType; 
      if([qt isEqualToString:@"Slider"]){ 
       ans.answer = currQ.sliderLabel.text; 
      }else if([qt isEqualToString:@"Option"]){    

      }else if([qt isEqualToString:@"TextInput"]){ 
       //NSLog(@"%@", currQ.inputAnswer); 
       ans.answer = currQ.inputAnswer.text; 
      }else if([qt isEqualToString:@"ImagePicker"]){ 

      }else{ 

      }      
      [answerArray addObject: ans]; 
      [ans release]; 
     } 
     [self startQuestion:currQNum isLast:FALSE];  
     currQNum++; 
    }else{ 
     ans.answer = currQ.sliderLabel.text; 
     [answerArray addObject: ans]; 

     //store data temporarily - section finished  
     [self startQuestion:currQNum isLast:TRUE];    
     currQNum++; 
    } 
    [ans release]; 
} 

-(void) finishQuestionnaire:(id)sender{ 
    //go back to main manual 
    //if else statement for answers 
    NSString* answ = currQ.sliderLabel.text; 
    [answerArray addObject: answ]; 
    [delegate finishedSection:answerArray section:secId]; 
    [answ release]; 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.questions = nil; 
    self.currQ = nil; 
    [super viewDidUnload]; 
} 

//hide back button in navigation bar 
- (void) viewWillAppear:(BOOL)animated{ 
    self.navigationItem.hidesBackButton = YES; 
} 

- (void)dealloc { 
    [currQ release]; 
    [db release]; 
    [questionArray release]; 
    [questions release]; 
    [super dealloc]; 
} 

@end 

有问题的行与上述是在switchViews函数。我需要让答案等于该问题视图中的特定输入组件(滑块值,文本输入值)。所以我需要使currQ成为可以使用任何视图控制器实例化的类型。

因此,我需要一个通用变量来保存当前问题。 currQ持有当前的问题,但目前是SliderQuestionViewController类型。我试图将其更改为id,但它引发了“请求成员...不是联合结构”的负载,还有一些错误分配的指针问题。

让我知道你是否需要更多的代码。

+0

[Generic Type In Objective C]的可能重复(http://stackoverflow.com/questions/6740873/generic-type-in​​-objective-c) –

回答

0

这看起来像你想要一个UIViewController的指针,所以只需使用它作为类型。然后你可以把它放到你以后喜欢的任何子类中。例如:

-(void)myAction:(UIViewController *)vc { 
    SpecialViewController *svc = (SpecialViewController *)vc; 
    ... 
} 

在你的情况,声明

UIViewController* currQ; 

,然后根据需要在实施访问您的两个子类的不同属性和方法投放。

+0

我做到了这一点,并且它导致请求的负载...像上面的错误(在switchView方法中)就好像它试图访问UIViewController中的成员而不是我的自定义视图控制器...我是不正确地分配currQ(currQ = [questionArray objectAtIndex:index];) – user559142

0

如果您正在寻找'通用'变量,您应该可以使用id。请确保您没有将类型定义为id*,星号应该是而不是

不过,更好的办法是为您的问题viewcontrollers创建一个超类。创建一个名为QuestionViewController的超类,它从UIViewController继承,并且具有从QuestionViewController继承的Slider和TextInput(以及任何其他)。然后,您可以将变量定义为:QuestionViewController* currQ;您也可以在该超类中添加任何通用功能,并消除重复。