0

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.

4
  • How are you declaring the scoreArray property? Are you including the retain directive? Otherwise I believe the default is assign. Commented Jan 27, 2011 at 0:58
  • First off, I would clean up the code by using the dot syntax and an autoreleased factory method of NSMutableArray: self.scoreArray = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",nil]; As to the actual problem, I'm not quite sure. Can you post the backtrace of the EXC_BAD_ACCESS error? Commented Jan 27, 2011 at 1:11
  • EXC_BAD_ACCESS is almost always indicative of a memory management error. Try running the static analyzer on your project - it excels at finding such bugs. Commented Jan 27, 2011 at 1:17
  • Yes, I'm using retain on the synthesized property. Commented Jan 27, 2011 at 4:47

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.