dreadbot
dreadbot

Reputation: 962

init an array in initWithNibName

I am trying to set up an array in my UIViewController. The nib gets loaded into my app, sometimes repeatedly. I am adding this to my initWithNibName to init the array

NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three",@"four",@"five",nil];
    [self setScoreArray:temp];
    [temp release]; 

scoreArray is a MutableArray with synthesized properties. When I go to access the array in viewDidLoad I get [[self scoreArray] count] as 0. Also, when I load the nib repeatedly I get a bad access error. Any suggestions? Am I missing a step. Thanks. I am synthesizing the property for the array, in my class declaration:

    NSMutableArray *_scoreArray;

then

@property (nonatomic, retain) NSMutableArray *scoreArray;

then in my implementation

@synthesize scoreArray =_scoreArray;

and in my dealloc

    [_scoreArray release], _scoreArray = nil;

I'm editing this post to show how I'm loading the nibs with my RootViewController

- (void) createNewSlide:(NSInteger)slideIndex {

NSDictionary *slideObj = (NSDictionary *)[[self slidesDataSource] objectAtIndex:slideIndex - 1];
NSString *currentTitle = [slideObj objectForKey:@"title"];
[[self slideTitle] setText:currentTitle];
NSString *currentContent = [slideObj objectForKey:@"contentString"];
NSString *currentContentType = [slideObj objectForKey:@"contentType"];

if ([self currentVC] != nil) {
        [[self currentVC] reset];
        [self setCurrentVC:nil];
}

if ([currentContentType isEqualToString:@"slide"]) 
{

    [[self containerViewController] loadImage:currentContent];

}
else if ([currentContentType isEqualToString:@"quiz"])
{
    NSInteger quizNum = [[slideObj objectForKey:@"quizNum"] integerValue];
    NSLog(@"%s%@%d",__FUNCTION__,@"quizNum ",quizNum);
    QuizViewController *quizView = [[QuizViewController alloc] initWithNibName:@"QuizViewController" bundle:nil];
    [self setCurrentVC:quizView];
     [quizView release];
    [[self containerViewController] replaceSlideWithViewController:[self currentVC]];
}
else if ([currentContentType isEqualToString:@"PIDView"])
{
    PIDViewController *PIDView = [[PIDViewController alloc] initWithNibName:@"PIDView" bundle:nil];
    [self setCurrentVC:PIDView];
    [PIDView release];
    [[self containerViewController] replaceSlideWithViewController:[self currentVC]];

}
else if ([currentContentType isEqualToString:@"LoginView"])
{
    LoginViewController *login = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
    [self setCurrentVC:login];
    [login release];
    [[self containerViewController] replaceSlideWithViewController:[self currentVC]];
}

else if ([currentContentType isEqualToString:@"VakView"])
{
    VakViewController *vakView = [[VakViewController alloc] initWithNibName:@"VakView" bundle:nil];
    [self setCurrentVC:vakView];
    [vakView release];
    [[self containerViewController] replaceSlideWithViewController:[self currentVC]];
}

else if ([currentContentType isEqualToString:@"PlanView"])
{
    PlanViewController *planView = [[PlanViewController alloc] initWithNibName:@"PlanView" bundle:nil];
    [self setCurrentVC:planView];
    [planView release];
    [[self containerViewController] replaceSlideWithViewController:[self currentVC]];   

}

Thanks for your insights. PlanView is the one that causes the problem. But it is not when it loads, it's when something else loads after it. I have run the analyzer and it reports no memory leaks. BTW, currentVC is a synthesized property.

Upvotes: 0

Views: 306

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185681

How is your UIViewController being created? If it is being created via another nib, then -initWithNibName:bundle: doesn't get called. Instead, -initWithCoder: and -awakeFromNib are called.

Upvotes: 3

Related Questions