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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 03-17-2010, 06:41 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default Group of buttons

If i want to put a large group of buttons into a single object that can be called easier at any time, what code would i use?
wardyfloyd is offline   Reply With Quote
Old 03-17-2010, 07:06 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by wardyfloyd View Post
If i want to put a large group of buttons into a single object that can be called easier at any time, what code would i use?
That depends on what you want to do with those buttons and why you think it is better to access them as a group rather than individually. One possibility is to define an ordinary C struct where the members of the struct are pointers to the individual buttons. Then that C struct could be an instance variable of some existing class, lilke a view controller, or the app delegate. But I don't see what advantage such a grouping would give you over simply putting all those buttons into an existing object.
RLScott is offline   Reply With Quote
Old 03-17-2010, 07:28 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default Creating buttons in a group

Quote:
Originally Posted by wardyfloyd View Post
If i want to put a large group of buttons into a single object that can be called easier at any time, what code would i use?
You can create a group of buttons in interface builder, select them, and choose "Embed objects in>view" from the layout menu.

You would then connect the view object to an outlet in your code, and then you could set the hidden property on the whole group of buttons to true to hide the buttons, or to false to show the buttons.

It's also possible to create one of these groups of buttons programmatically from a nib file and add it to a view at runtime. That will save you the trouble of creating your buttons through code, which can be a challenge if you're just getting started in iPhone development. Do a search on "Loading Custom Table-View Cells From Nib Files" in the XCode developer documentation system for an explanation of this technique. That section is written to describe loading a custom table view cell, but it can be generalized to load any pre-defined object from a nib file.

You could also write a method that creates UIButton objects one at a time through code, configures them, and then creates a view object, and adds the buttons to the view as subviews using the UIView method -addSubview.

I don't have any code lying around that does exactly what you want, so you're going to have to read up and puzzle through it on your own.


Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 03-20-2010, 08:19 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

Let me make this easier. I want to put all of these buttons into one object and create a randomizer for there X and Y properties so that every time they reappear they reappear in different places. I would love some help with all of that.
wardyfloyd is offline   Reply With Quote
Old 03-20-2010, 08:22 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

here is the code


- (void)loadView {

if (self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]);

int x = rand() % 230;
int y = rand() % 426;
int x1 = rand() % 230;
int y1 = rand() % 426;
int x2 = rand() % 230;
int y2 = rand() % 426;
int x3 = rand() % 230;
int y3 = rand() % 426;


self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

self.view.backgroundColor = [UIColor whiteColor];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button1.frame = CGRectMake(x, y, 70, 20);

[button1 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button1 addTarget:self action:@selector(buttonPressed
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button1];


UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button2.frame = CGRectMake(x1, y1, 100, 30);

[button2 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button2 addTarget:self action:@selector(buttonPressed1
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button3.frame = CGRectMake(x2, y2, 100, 40);

[button3 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button3 addTarget:self action:@selector(buttonPressed2
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button3];

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button4.frame = CGRectMake(x3, y3, 90, 34);

[button4 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button4 addTarget:self action:@selector(buttonPressed3
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button4];



}
wardyfloyd is offline   Reply With Quote
Old 03-20-2010, 08:25 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

-(void)buttonPressed: (UIButton*)button {

[button setHidden:YES];
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(showButton: )
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:button, @"button", nil]
repeats:NO];


}

- (void)showButton: (NSTimer*)timer {
[[[timer userInfo] objectForKey:@"button"] setHidden:NO];
}


-(void)buttonPressed1: (UIButton*)button {

[button setHidden:YES];
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(showButton1: )
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:button, @"button", nil]
repeats:NO];


}

- (void)showButton1: (NSTimer*)timer {
[[[timer userInfo] objectForKey:@"button"] setHidden:NO];
}


-(void)buttonPressed2: (UIButton*)button {

[button setHidden:YES];
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(showButton2
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:button, @"button", nil]
repeats:NO];


}

- (void)showButton2NSTimer*)timer {
[[[timer userInfo] objectForKey:@"button"] setHidden:NO];
}

-(void)buttonPressed3UIButton*)button {

[button setHidden:YES];
[NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(showButton3: )
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:button, @"button", nil]
repeats:NO];


}

- (void)showButton3NSTimer*)timer {
[[[timer userInfo] objectForKey:@"button"] setHidden:NO];
}



- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


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


@end
wardyfloyd is offline   Reply With Quote
Old 03-20-2010, 08:30 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default You need randomizing code in your -viewWilAppear method

Quote:
Originally Posted by wardyfloyd View Post
here is the code


- (void)loadView {

if (self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]);

int x = rand() % 230;
int y = rand() % 426;
int x1 = rand() % 230;
int y1 = rand() % 426;
int x2 = rand() % 230;
int y2 = rand() % 426;
int x3 = rand() % 230;
int y3 = rand() % 426;


self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

self.view.backgroundColor = [UIColor whiteColor];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button1.frame = CGRectMake(x, y, 70, 20);

[button1 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button1 addTarget:self action:@selector(buttonPressed
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button1];


UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button2.frame = CGRectMake(x1, y1, 100, 30);

[button2 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button2 addTarget:self action:@selector(buttonPressed1
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button2];

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button3.frame = CGRectMake(x2, y2, 100, 40);

[button3 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button3 addTarget:self action:@selector(buttonPressed2
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button3];

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button4.frame = CGRectMake(x3, y3, 90, 34);

[button4 setTitle:@"Tap Me" forState:UIControlStateNormal];

[button4 addTarget:self action:@selector(buttonPressed3
forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button4];



}
At a glance, it looks like you've written a lot of the code. However, having this code in your -viewDidLoad means the buttons will only be randomized when the view is loaded (which only happens the first time the view is displayed, or after a low memory condition.

Instead, you should probably put code that randomizes your button location in your viewDidAppear method.

You can simply change the frame or center property of each button and the system will redraw it at its new location.

Your code doesn't seem to do anything to avoid buttons overlapping or covering each other.

I would suggest mapping out a grid of locations for your buttons so they land in random grid spots, but always centered in a grid spot. Make the grid locations big enough for the largest button. That will let you avoid having buttons land on top of each other. It won't look as random as having totally random x,y positions, but it would be a lot simpler than coming up with a way to make them random but NOT overlap. That would take some real thought.


Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 03-20-2010, 09:26 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

thanks for the advice Duncan, but I don't know if this code will transfer over from loadview to viewdidappear. how would i actually transfer this code?
wardyfloyd is offline   Reply With Quote
Old 03-20-2010, 09:28 PM   #9 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

when i try changing loadview into viewdidappear or loadviewdidappear, the screen is white when i open the application.
wardyfloyd is offline   Reply With Quote
Old 03-20-2010, 09:39 PM   #10 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 4,814
Default

Quote:
Originally Posted by wardyfloyd View Post
thanks for the advice Duncan, but I don't know if this code will transfer over from loadview to viewdidappear. how would i actually transfer this code?

Floyd,

You need to extract the code that changes the button's frames and put it in the -viewWillAppear method.

You should be able to leave -vewDidLoad as it is, and simply add code to -viewWillAppear that generates random numbers and applies those numbers to the frame.origin of each of your buttons.

Something like this:

Code:
- (void)viewWillAppear:(BOOL)animated
{
   CGPoint newOrigin;
   CGRect buttonFrame;

   for (index = 0; index < button_count; index++)
   {
      newOrigin.x = rand() % xRange; //fix this code to set your x range  
      newOrigin.y = rand() % yRange; //Fix this code to set your y range
      buttonFrame  = button_array[index].frame;
      buttonFrame.origin = newOrigin;
      button_array[index].frame = buttonFrame;
    }
}
That code is rough, and isn't ready to use. It assumes you have an array of button objects in button_array.
A button is a view, and a view's frame is defined in terms of it's superView (the view it's inside.) You will need to figure out the maximum and minimum origins for your buttons so that they stay inside their superview.

The view that contains the buttons will have a bounds property that gives you the rectangle the buttons should be placed inside, using the same numbering system as the frame property of each button. So you will need to juggle your button origins so their value starts at the superview's bounds.origin, and is never greater than the superview's right or bottom edges.

That should be enough guidance for you to get started. I'm under a pretty tight deadline right now, and that's the best I can give you. You're going to have to roll up your sleeves and figure out how to write this code yourself.


Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 03-22-2010, 10:18 PM   #11 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 45
Default

Im afraid i still need assistance (from someone other than duncan (good luck with ur deadline)). I don't know how to set up an array for a group of buttons and implement it in this code. I am a java user and this is my first foray into objective c.
wardyfloyd is offline   Reply With Quote
Reply

Bookmarks

Tags
button, group, method call, object, 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: 273
20 members and 253 guests
@sandris, AdamL, ADY, Dani77, diyora, FAED, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,750
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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