0

I've been trying for hours to try and get this to work, but I can't seem to do it. The user pressed a button, which calls 'newPalette' in HandlingPalettes, and then pushes in SingleView. Here's all the revelant code I have:

HandlingPalettes.h:

@interface HandlingPalettes : UIViewController {

NSMutableArray *navBarColour;

}

@property (nonatomic, retain) NSMutableArray *navBarColour;

-(void)newPalette;

@end

HandlingPalettes.m:

#import "HandlingPalettes.h"
#import "SingleView.h"



@implementation HandlingPalettes

@synthesize navBarColour;


-(void)newPalette {

    UIColor *colourOfNavBar = [UIColor colorWithHue:0 saturation:0 brightness:0.25 alpha:1];
    if (navBarColour == nil) {
        navBarColour = [[NSMutableArray alloc] initWithObjects:colourOfNavBar, nil];
        currentPalette = 0;
    }
    else {
        [navBarColour addObject:colourOfNavBar];
        currentPalette = navBarColour.count-1;
    }

    NSLog(@"Number: %i", navBarColour.count);

}

- (void)dealloc {
    [super dealloc];
}
@end

SingleView.h:

#import "HandlingPalettes.h"


@interface SingleView : UIViewController {

}

HandlingPalettes *handlingPalettes;

@end

SingleView.m:

#import "SingleView.h"

@implementation SingleView


- (void)viewDidLoad {

    handlingPalettes = [[HandlingPalettes alloc] init];
    NSLog(@"Second number: %i", handlingPalettes.navBarColour.count);
    [super viewDidLoad];

}

- (void)dealloc {
    [handlingPalettes release];
    [super dealloc];
}


@end

The problem I have is that NSLog returns:

Number: 1 Second Number: 0

And then going back to the first view and pressing the button again..

Number: 2 Second Number: 0

And again..

Number 3: Second Number: 0

Can somebody help me and explain why this isn't working?

Thanks so much.

3
  • It should pass on the array so that the count is the same in both areas. Commented Mar 9, 2011 at 4:09
  • post the method that is invoked when you press the button Commented Mar 9, 2011 at 4:13
  • however I'm pretty sure that you just push the newly created view controller. That is what KingofBliss answered. He is right. Commented Mar 9, 2011 at 4:14

2 Answers 2

4

You are creating a different instance to HandlingPalettes class. You should use singleton to do so.

handlingPalettes in handlingPalettes.m and handlingPalettes in SingleView are always different. So use singleton class, or use appDelegate to access in different class.

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

3 Comments

+1,Yes, making object in different class reinitalize the array.
I don't understand, can you explain this more?
You are creating a new instance to the class handlingPalettes, this will allocate a new memory location for handlingPalettes.color, But the old one will be in some other memory location. So it produces different value
0

you need to replace this line in HandlingPalettes.m

navBarColour = [[NSMutableArray alloc] initWithObjects:colourOfNavBar, nil];

with this

self.navBarColour = [[NSMutableArray alloc] initWithObjects:colourOfNavBar, nil];

And you need change here

@interface SingleView : UIViewController {

}

HandlingPalettes *handlingPalettes; //not here

@end

correct

 @interface SingleView : UIViewController {

   HandlingPalettes *handlingPalettes;    

    }



    @end

Edit:

Because it reinitialize the array.so you need either you make this array in same class or make in appDelegate class. because app delegate class not reintialize it.

1 Comment

Unfortunately after making these changes, it returned the same result.

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.