0

I am trying to build an NSMutableArray each time a button is pressed. I am using NSLog to check the array count after each button pressed and the value is zero. THe code is below.

ProjectViewController.h

NSMutableArray *numberQuery
...
@property (nonatomic, retain) NSMutableArray *numberQuery;

ProjectViewController.m

- (void)makeButtons{
UIButton * pickButton;
int y_plot = 150;
int x_plot = 70;
int z = 0;

for(int y = 1; y < 10; y++)
{

    for(int x = 1; x < 5; x++){
        z++;

        pickButton = [UIButton buttonWithType:UIButtonTypeCustom];
        pickButton.frame = CGRectMake(x*x_plot, y_plot, 60, 40);

        [pickButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"]     forState:UIControlStateNormal];
        [pickButton addTarget:self action:@selector(digitClick:) forControlEvents:UIControlEventTouchUpInside];
        [pickButton setTitle:[NSString stringWithFormat:@"%d",z] forState:UIControlStateNormal];
        pickButton.titleLabel.textColor = [UIColor blackColor]; 
        pickButton.tag = z;
        [self.view addSubview:aButton];
    }
    y_plot=y_plot+45;
  }
}

//************************
- (void)digitClick:(id)sender{
UIButton * chosenButton =(UIButton *)sender;

if ([sender isSelected] ==FALSE) {  
    [sender setSelected:TRUE];
    [chosenButton setBackgroundImage:[UIImage imageNamed:@"btnSelected.png"] forState:UIControlStateNormal];
    chosenButton.titleLabel.textColor = [UIColor whiteColor];

    if([queryNumbersX count]<6){
        [self numberSearchArray:chosenButton.tag];
        }
    }
else
    {[sender setSelected:FALSE];
    [chosenButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"]forState:UIControlStateNormal];
    chosenButton.titleLabel.textColor = [UIColor blackColor];
}

forState:UIControlStateNormal];
NSLog(@"clicked button with title %d",chosenButton.tag); 
}

//************************
-(void) numberSearchArray:(NSInteger)newNumber;
{

   [self.numberQuery addObject:[NSNumber numberWithInt: newNumber]];
   NSLog(@"numberSearchArray %d - count %d",newNumber, [self.numberQuery count]);  
}

Am I using the NSMutableArray in the right manner...declaration?

This is the code in the viewDidLoad method

NSMutableArray *numberQuery = [[NSMutableArray alloc] initWithObjects:nil];

Although I have the array declared int he header file, It seems as though I cannot access it outside of the method in which it was allocated.

2 Answers 2

1
@property (nonatomic, retain) NSMutableArray *numberQuery;

You must balance that with

@synthesize numberQuery;

in the .m

and the correct creation would be

self.numberQuery = [NSMuatableArray arrayWithCapacity:someNumber];

By doing this

NSMutableArray *numberQuery = [[NSMutableArray alloc] initWithObjects:nil];

You are not using your @property you are creating a new variable, this is why you get the warning that your variable is not use, because it's scope is actually just the viewDidLoad method instead of the object.

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

4 Comments

you can also use a convenient method self.numberQuery = [NSMutableArray array];. This array is autoreleased, so your retaining property is balanced. Also using the initWithObjects: and just passing nil look suspicious.
@vikingosegundo you are right I will modify it for + (id)arrayWithCapacity:(NSUInteger)numItems that would be better
THanks, VinceBurn! Worked perfectly now!
The best about convenient factory methods: you don't have to worry, if ARC is enabled.
1

It looks like that you have never alloced numberQuery. So it is always nil and thus addObject method is ignored. You need to alloc this in init (or any suitable place you like) and release in dealloc (or in other suitable place).

2 Comments

I actually allocate it in the viewDIdLoad method. I get a warning message stating that the variable is never used.
I hav added the allocation statement to the original question.

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.