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.
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.
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.
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;
You need randomizing code in your -viewWilAppear method
Quote:
Originally Posted by wardyfloyd
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;
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.
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?
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.
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.