объекты в nsmutablearray становятся зомби - PullRequest
0 голосов
/ 20 декабря 2011

я разрабатываю очень простое приложение для викторины

В viewDidLoad я добавляю объекты в myarray

, где бы я ни записывал значения myarray, он отлично работает

, но если я попробую этовнутри методов ibaction все объекты становятся зомби

на 2 дня. Я застрял в этом, но не могу найти, что не так.

quiz.h

#import <UIKit/UIKit.h>
#import <sqlite3.h>

@class dbVals;
@class viewTransition;
@class AppDelegate;

@interface quiz : UIViewController
{
    NSMutableArray *myarray;

    IBOutlet UITextView *questionTextView_;

    IBOutlet UIButton *skipButton_;

    IBOutlet UIButton *optionAButton_;

    IBOutlet UIButton *optionBButton_;

    IBOutlet UIButton *optionCButton_;

    NSString *correctAnswer;

    int questionNumber;

    int score;

    IBOutlet UILabel *scoreLabel_;

    int totalQuestions;

}

-(void)populate:(int)number;

//@property(nonatomic, retain) NSMutableArray *myarray;

@property (retain, nonatomic) IBOutlet UITextView *questionTextView;

@property (retain, nonatomic) IBOutlet UIButton *skipButton;

@property (retain, nonatomic) IBOutlet UIButton *optionAButton;

@property (retain, nonatomic) IBOutlet UIButton *optionBButton;

@property (retain, nonatomic) IBOutlet UIButton *optionCButton;

@property (retain, nonatomic) IBOutlet UILabel *scoreLabel;

- (IBAction)optionsToAnswer:(id)sender;

- (IBAction)zzz:(id)sender;

@end

quiz.m

#import "quiz.h"
#import "DbVals.h"
#import "viewTransition.h"
#import "AppDelegate.h"

@implementation quiz
@synthesize skipButton=skipButton_;
@synthesize optionAButton=optionAButton_;
@synthesize optionBButton=optionBButton_;
@synthesize optionCButton=optionCButton_;
@synthesize scoreLabel=scoreLabel_;
@synthesize questionTextView=questionTextView_;

//@synthesize myarray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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.
}

#pragma mark - View lifecycle

- (void)createEditableCopyOfDatabaseIfNeeded
{
    //NSLog(@"Creating editable copy of database");
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"oq.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"oq.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

+(sqlite3 *) getNewDBConnection
{
    sqlite3 *newDBconnection;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"oq.sqlite"];
    // Open the database. The database was prepared outside the application.
    if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {

        //NSLog(@"Database Successfully Opened  ");

    } else {
        NSLog(@"Error in opening database  ");
    }

    return newDBconnection; 
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    questionNumber = 0;

    score = 0;

    [self createEditableCopyOfDatabaseIfNeeded];    

    sqlite3 *dbc = [quiz getNewDBConnection];

    sqlite3_stmt *statement = nil;

    const char *sqlSelect = "select * from QnA ORDER BY RANDOM()";

    if(sqlite3_prepare_v2(dbc, sqlSelect, -1, &statement, NULL)!=SQLITE_OK)
    {
        NSAssert1(0, @"Error Preparing Statement", sqlite3_errmsg(dbc));
    }
    else
    {
        myarray = [[NSMutableArray alloc]init];

        while(sqlite3_step(statement)==SQLITE_ROW)
        {
            NSString *q = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 0)];
            NSString *o = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)];
            NSString *a = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 2)];

            DbVals *dbValsObj = [[DbVals alloc]init];            
            [dbValsObj setValsOfQuestions:q options:o answer:a];                
            [myarray addObject:dbValsObj];            
            [dbValsObj release];          
        }
    }

    sqlite3_finalize(statement);

    //[self populate:questionNumber];

}

-(void)populate:(int)number
{
    /*[scoreLabel_ setText:[NSString stringWithFormat:@"%d",score]];

    AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
    [appDel setFinalScore:[NSString stringWithFormat:@"%d",score]];

    if(number < [myarray count])
    {
        DbVals *dbv1 = [myarray objectAtIndex:number];

        [questionTextView_ setText:[dbv1 getQuestions]];

        NSString *joinedOptions = [dbv1 getOptions];

        NSArray *splitOptions = [joinedOptions componentsSeparatedByString:@","];
        [optionAButton_ setTitle:[splitOptions objectAtIndex:0] forState:UIControlStateNormal];
        [optionBButton_ setTitle:[splitOptions objectAtIndex:1] forState:UIControlStateNormal];
        [optionCButton_ setTitle:[splitOptions objectAtIndex:2] forState:UIControlStateNormal];

        correctAnswer = [dbv1 getAnswer];
    }
    else
    {
        //viewTransition *vt = [[viewTransition alloc]init];
        [viewTransition viewsTransitionCurrentView:self toNextView:@"result"];
        //[vt release];       
    }*/
}

- (void)viewDidUnload
{
    for(int i=0; i<[myarray count]; i++)
    {
        DbVals *dbv1 = [myarray objectAtIndex:i];
        NSLog(@"%@",[dbv1 getQuestions]);
        NSLog(@"%@",[dbv1 getOptions]);
        NSLog(@"%@",[dbv1 getAnswer]);
        NSLog(@"<u><u><u><u><><><><><><><><><><><>");
    }

    [self setQuestionTextView:nil];
    [questionTextView_ release];
    questionTextView_ = nil;
    [self setQuestionTextView:nil];
    [skipButton_ release];
    skipButton_ = nil;
    [self setSkipButton:nil];
    [optionAButton_ release];
    optionAButton_ = nil;
    [self setOptionAButton:nil];
    [optionBButton_ release];
    optionBButton_ = nil;
    [self setOptionBButton:nil];
    [optionCButton_ release];
    optionCButton_ = nil;
    [self setOptionCButton:nil];
    [scoreLabel_ release];
    scoreLabel_ = nil;
    [self setScoreLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc
{
    for(int i=0; i<[myarray count]; i++)
    {
        DbVals *dbv1 = [myarray objectAtIndex:i];
        NSLog(@"%@",[dbv1 getQuestions]);
        NSLog(@"%@",[dbv1 getOptions]);
        NSLog(@"%@",[dbv1 getAnswer]);
        NSLog(@"<d><d><d><d><d><><><><><><><><><><>");
    }

    [questionTextView_ release];

    [skipButton_ release];

    [optionAButton_ release];

    [optionBButton_ release];

    [optionCButton_ release];

    [scoreLabel_ release];

    [myarray release];

    [super dealloc];
}

- (IBAction)optionsToAnswer:(id)sender
{
    for(int i=0; i<[myarray count]; i++)
    {
        DbVals *dbv1 = [myarray objectAtIndex:i];
        NSLog(@"%@",[dbv1 getQuestions]);
        NSLog(@"%@",[dbv1 getOptions]);
        NSLog(@"%@",[dbv1 getAnswer]);
        NSLog(@"six");
    }


    if(sender == skipButton_)
    {
        //questionNumber++;

        //[self populate:questionNumber];

        /*[UIView animateWithDuration:5 delay:0 options: UIViewAnimationCurveEaseOut
        animations:
        ^{
            [UIView setAnimationTransition:103 forView:self.view cache:NO]; 
        } 
        completion:
        ^(BOOL finished)
        {                             
        }
        ];*/
    }

    if(sender == optionAButton_)
    {
        /*NSString *one = @"1";
        if([correctAnswer isEqualToString:one])
        {
            score++;
        }
        questionNumber++;
        [self populate:questionNumber];*/
    }

    if(sender == optionBButton_)
    {
        /*NSString *two = @"2";
        if([correctAnswer isEqualToString:two])
        {
            score++;
        }
        questionNumber++;
        [self populate:questionNumber];*/
    }

    if(sender == optionCButton_)
    {
        /*NSString *three = @"3";
        if([correctAnswer isEqualToString:three])
        {
            score++;
        }
        questionNumber++;
        [self populate:questionNumber];*/
    }
}

- (IBAction)zzz:(id)sender 
{
}
@end

dbVals.h

#import <Foundation/Foundation.h>

@interface DbVals : NSObject
{
    NSString *questions_;
    NSString *options_;
    NSString *answer_;
    //    NSString *hint;
    //    NSString *mode;
}

-(void)setValsOfQuestions:(NSString*)questions options:(NSString*)options answer:(NSString*)answer;

-(NSString*)getQuestions;
-(NSString*)getOptions;
-(NSString*)getAnswer;

dbVals.m

#import "DbVals.h"

@implementation DbVals

-(void)setValsOfQuestions:(NSString*)questions options:(NSString*)options answer:(NSString*)answer
{
    questions_ = questions;
    options_ = options;
    answer_ = answer;
}

-(NSString*)getQuestions
{
    return questions_;
}

-(NSString*)getOptions
{
    return options_;
}

-(NSString*)getAnswer
{
    return answer_;
}

@end

1 Ответ

0 голосов
/ 20 декабря 2011

Ваш dbVals.m setVals не сохраняет параметры.Это, очевидно, означает, что все внутри становится освобожденным, как только заканчивается область действия функции.Попробуйте изменить его на что-то вроде

-(void)setValsOfQuestions:(NSString*)questions options:(NSString*)options answer:(NSString*)answer
{
    [questions_ release];
    [options_ release];
    [answer_ release];
    questions_ = [questions copy];
    options_ = [options copy];
    answer_ = [answer copy];
}
...