Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

minofifa

macrumors newbie
Original poster
Oct 24, 2008
5
0
Hi everybody

I'm trying to create a View that partitions the entire iPhone surface into a grid of squares, where each square can be touched to generate an event. Basically I want it to look like the Minesweeper board (no i'm not writing minesweeper or anything like it).

I'm new to this framework so I'm not sure on the correct approach to take. I basically want to make a bunch of UIButtons, but I need to do it programatically (not by laying down a bunch using Interface Builder). Further, I dont want the look of a UIButton, I just want a plain square that can send a message when touched.

Thank you for any help.
 
Check out the docs for UIButton, UIControl and UIView. The latter 2 are super classes of UIButton.

You will create a new button like this, with this method declared in UIView:

Code:
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,32)

Then check out the docs for UIControl (the superclass of UIButton) for methods that let you set the actions and targets the button will trigger. Namely:

Code:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

So you would write something like:
Code:
[myButton addTarget:someObject action:@selector(doSomething) forControlEvents:UIControlEventTouchUpInside]

Now use the methods define the UIButton docs to change the look of the button. Namely things like setImage:forState: and it's friends.

Lastly, add it to an existing view. If this is in a view controller, you probably have an object named view as a property of the viewcontroller.

Code:
[self.view addSubview:myButton]

Wrap all that up in a nested loop to handle rows and columns and you should be well on your way. The moral of this story is the documentation is awesome. Read it about your class, and don't forget to search super classes if you can't find what you need.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.