2011-07-19 124 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;您也可以在該超類中添加任何通用功能,並消除重複。