I've been learning to code the iPhone by example and there are some things I'm getting, and somethings I'm missing.. this is one of the things I can't figure out.
I have an NSMutableArray which is read from a Plist file and then I make
Later I want to remove these buttons, so I need to have a reference to them ... which I guess means I need to put them in the header file... but I don't know how to do this, as my dynamic array size cannot be defined in there (can it?)
Many thanks for reading and sorry for the n00by question.
I understand that adding the buttons to another array would enable me to keep a reference of them, but I can't seem to get rid of them... this is what I'm trying:
I added the buttons to an array with this line
Code:
[arr_buttons addObject:buttons[j]];
and now I'm calling the following method
Code:
-(void)updateButtons
{
int j=0;
for(j = 0; j<numberOfButtons; j++){
NSLog(@"debug message to prove this is being called");
[arr_buttons objectAtIndex:j].hidden = YES;
}
}
I see the debug message, but the buttons don't get hidden :-(
I understand that adding the buttons to another array would enable me to keep a reference of them, but I can't seem to get rid of them... this is what I'm trying:
I added the buttons to an array with this line
Code:
[arr_buttons addObject:buttons[j]];
and now I'm calling the following method
Code:
-(void)updateButtons
{
int j=0;
for(j = 0; j<numberOfButtons; j++){
NSLog(@"debug message to prove this is being called");
[arr_buttons objectAtIndex:j].hidden = YES;
}
}
I see the debug message, but the buttons don't get hidden :-(
How do I "go through the subviews"?
Why not
Code:
for (UIButton* button in numberOfButtons) {
[button removeFromSuperview];
}
The method you suggested is new to me... I'm not sure how it's able to tell what buttons it is removing.. is this ambiguous if I have added other buttons to the view?
Thanks again!
EDIT: actually I just get an error... it doesn't like
Code:
for (UIButton* button in numberOfButtons) {
it throws a "expression does not have a valid object type" error...
The method you suggested is new to me... I'm not sure how it's able to tell what buttons it is removing.. is this ambiguous if I have added other buttons to the view?
Thanks again!
EDIT: actually I just get an error... it doesn't like
Code:
for (UIButton* button in numberOfButtons) {
it throws a "expression does not have a valid object type" error...
Did you declare the buttons as *button? or *buttonS?
buttons. but even if i change to buttons i still get that error..
Not sure the,. I used this
Code:
plop = [[UIImageView alloc] initWithFrame:myImageRect];
...
[thrownPlops addObject:plop];
//
for (UIView* view in thrownPlops) {
[view removeFromSuperview];
}
to remove a series of imageViews. I assumed it would be something similar for buttons.
EDIT: Looking back at what you wrote - thrownPlops is an array in my code and I was assuming that numberOfButtons was an array, but it isn't. Try myArray.
It is very hard to fix bugs when code is presented piecemeal. Please provide the full set of code when making changes.
Quote:
Originally Posted by lenstar
I understand that adding the buttons to another array would enable me to keep a reference of them, but I can't seem to get rid of them... this is what I'm trying:
I added the buttons to an array with this line
Code:
[arr_buttons addObject:buttons[j]];
and now I'm calling the following method
Code:
-(void)updateButtons
{
int j=0;
for(j = 0; j<numberOfButtons; j++){
NSLog(@"debug message to prove this is being called");
[arr_buttons objectAtIndex:j].hidden = YES;
}
}
I see the debug message, but the buttons don't get hidden :-(
Need to see the full text of how they are being added to the array and the view to figure out the issue. I'm not seeing the problem here.
Quote:
Originally Posted by lenstar
How do I "go through the subviews"?
Subviews is a UIView property. See the documentation.
plop = [[UIImageView alloc] initWithFrame:myImageRect];
...
[thrownPlops addObject:plop];
//
for (UIView* view in thrownPlops) {
[view removeFromSuperview];
}
to remove a series of imageViews. I assumed it would be something similar for buttons.
EDIT: Looking back at what you wrote - thrownPlops is an array in my code and I was assuming that numberOfButtons was an array, but it isn't. Try myArray.
Yes, this is similar to what I need, execpt "thrownplops" is in a different method than the removal of thrownplops (see code below)
Quote:
Originally Posted by BrianSlick
It is very hard to fix bugs when code is presented piecemeal. Please provide the full set of code when making changes.
Need to see the full text of how they are being added to the array and the view to figure out the issue. I'm not seeing the problem here.
Subviews is a UIView property. See the documentation.
Here is the whole code
Code:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *buttonsPath = [[NSString alloc] initWithFormat:@"%@/buttons.plist", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
buttonsArray = [[NSMutableDictionary alloc] initWithContentsOfFile:buttonsPath];
numberOfbuttons = [buttonsArray count];
[self drawbuttons];
[self updatebuttons];
}
-(void)drawbuttons
{
numberOfButtons = [myArray count];
UIButton *buttons[numberOfButtons];
for(j = 0; j<numberOfButtons; j++){
[arr_buttons addObject:buttons[j]]; //this is defined in the header as an NSMutableArray
// button parameters coded here
[self.view addSubview:buttons[j]];
}
}
-(void)updatebuttons
{
for (UIButton* buttons in arr_buttons) {
[buttons removeFromSuperview];
}
}
I hope that makes the problem clearer.
Thanks,
Andy