Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-11-2010, 01:57 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default Creating an array of n UIbuttons

Hi all,

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
Code:
[myArray count]
buttons, like so:

Code:
numberOfButtons = [myArray count];
UIButton *buttons[numberOfButtons];
for(j = 0; j<numberOfButtons; j++){
   // button parameters coded here
   [self.view numberOfButtons[j]];
}
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.

Andy
lenstar is offline   Reply With Quote
Old 04-11-2010, 03:01 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Add them to another array as you add them to the view. Then you'll keep a reference to them.

Or, you can find them later by going through the subviews.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-11-2010, 03:52 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default

Thanks for the reply, BrianSlick :-)

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"?
lenstar is offline   Reply With Quote
Old 04-11-2010, 03:57 PM   #4 (permalink)
Ascending Mt. SDK
 
Join Date: Dec 2009
Posts: 64
gbaldwin9 is on a distinguished road
Default

Quote:
Originally Posted by lenstar View Post
Thanks for the reply, BrianSlick :-)

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];
			}
?
gbaldwin9 is offline   Reply With Quote
Old 04-11-2010, 04:31 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default

Thanks for the reply gbaldwin9 :-)

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...

Last edited by lenstar; 04-11-2010 at 04:36 PM.
lenstar is offline   Reply With Quote
Old 04-11-2010, 04:38 PM   #6 (permalink)
Ascending Mt. SDK
 
Join Date: Dec 2009
Posts: 64
gbaldwin9 is on a distinguished road
Default

Quote:
Originally Posted by lenstar View Post
Thanks for the reply gbaldwin9 :-)

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?
gbaldwin9 is offline   Reply With Quote
Old 04-11-2010, 04:47 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default

Quote:
Originally Posted by gbaldwin9 View Post
Did you declare the buttons as *button? or *buttonS?
buttons. but even if i change to buttons i still get that error..
lenstar is offline   Reply With Quote
Old 04-11-2010, 05:29 PM   #8 (permalink)
Ascending Mt. SDK
 
Join Date: Dec 2009
Posts: 64
gbaldwin9 is on a distinguished road
Default

Quote:
Originally Posted by lenstar View Post
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.

Last edited by gbaldwin9; 04-11-2010 at 05:35 PM.
gbaldwin9 is offline   Reply With Quote
Old 04-11-2010, 05:55 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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 View Post
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 View Post
How do I "go through the subviews"?
Subviews is a UIView property. See the documentation.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-12-2010, 03:48 AM   #10 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default

Quote:
Originally Posted by gbaldwin9 View Post
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.

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 View Post
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

Last edited by lenstar; 04-12-2010 at 05:28 PM.
lenstar is offline   Reply With Quote
Old 04-12-2010, 06:41 AM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

buttonsPath is leaked.

Why are you mixing C arrays and NS(Mutable)Arrays?

What are you expecting drawbuttons to actually do? It's not really doing anything at the moment.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-12-2010, 05:28 PM   #12 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 6
lenstar is on a distinguished road
Default

Sorry, I've edited it now and now it adds the buttons to my view.
lenstar is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, uibutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 317
15 members and 302 guests
chemistry, Domele, Fstuff, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, kapps11, newDev, SLIC, stanny, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,874
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:26 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0