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

IDMah

macrumors 6502
Original poster
May 13, 2011
317
11
Hi all.

I can't seem to figure out how I would create a button template
ie. a button with labels in each corner and one in the centre.

and then create a matrix of 9 x 13 of those buttons.
also how do you access the matrix without having 117 IBOutlets ?

Could I do it in IB or would it be better to create it programmatically?

thanks
Ian

ps. Should the button object be a UIViewClass ?? or an NSObject..
pss. I did call it a NUBE question.. :)
 
how do you access the matrix without having 117 IBOutlets ?

Could I do it in IB or would it be better to create it programmatically?

You can have multiple buttons connect to a single IBAction method.

You'd set it up like:

Code:
- (IBAction) buttonPressed:(UIButton *)sender;

You can then use sender to figure out which button was hit. I think in your case the best solution would be something like:

Code:
switch (sender.tag)
{
    case 0:[...]

Tag is an int that you can assign separately to each button in IB.
 
You mean you want to make an Gridview? You can just make them in code, with some simple math calculations, and then
Code:
for (UIButton *button in self.view.subviews) {
 [button addTarget:@selector(something];
}

I don't have XCode open right now, so don't just copy paste what I wrote, but think about it, because 117 outlets is indeed a bit of waste.
 
ahhh yes... but now..

Thanks gang.. I was going to use NSMatrix but I think that the game will require more exact placing of the buttons / images.. so I'm going to use something like this in a loop.

Code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(X1,40, Y1, 40);
[view addSubview:button];

but one question that spring to mind is how do I access all the buttons?
if I make 117 buttons how do I access or get the events I'd like to get receive a value when the button is pushed..

*Also* is it possible to change a button type 'on the fly' from Sticky to 'push' and vice versa.

thanks.
Ian

ps. Since the labels are for Diagnostics anyways I'm just going to stack the
text instead of rendering creating some fancy four label button. just a
thought couldn't I create an image by converting NSRectangle ? or is that
too old school way of thinking?
 
I was going to use NSMatrix but...
Good thing you abandoned this option. NSMatrix is not available for iOS.

Code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake[COLOR="Red"](X1,40, Y1, 40)[/COLOR];
[view addSubview:button];
Might want to review the parameters and their order for CGRectMake. Doesn't look like you are using it correctly.

but one question that spring to mind is how do I access all the buttons?
if I make 117 buttons how do I access or get the events I'd like to get receive a value when the button is pushed..
If you need access to these buttons after this loop, you should probably save references to them. As for "getting the events", that is exactly what adding the target to the button sets up for you.

*Also* is it possible to change a button type 'on the fly' from Sticky to 'push' and vice versa.
Not sure what you mean by sticky vs. push. Care to elaborate?

ps. Since the labels are for Diagnostics anyways I'm just going to stack the
text instead of rendering creating some fancy four label button. just a
thought couldn't I create an image by converting NSRectangle ? or is that
too old school way of thinking?
Seems rather old school to me. If you don't want to subclass your button, perhaps consider adding your extra labels as subviews.
 
push vs. Sticky buttons

Thanks I'll double check the CGRectMake info.

Code:
// yeap ... (positionX, positionY, sizeX, SizeY)
button.frame = CGRectMake(X1, Y1, 40, 40);

A push button is like the ok button.. it activates when you release on it..

the Sticky is like a push button switch. push to turn it on..
(a graphic could light it up) or change colour it .. "Sticks" on
and when you click on it again it goes back to normal again..

Let me guess, that's not available on the iphone either..

??? Subviews ???

thanks.
Ian

ps. found those buttons in here.
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Button/Button.pdf
 
Last edited:
A push button is like the ok button.. it activates when you release on it..

the Sticky is like a push button switch. push to turn it on..
(a graphic could light it up) or change colour it .. "Sticks" on
and when you click on it again it goes back to normal again..

Let me guess, that's not available on the iphone either..

...

ps. found those buttons in here.
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Button/Button.pdf
When you're coding an iOS app, you should probably avoid documentation for Mac apps and stick with iOS documentation. In other words: UIButton in this case. You'll find info about its various states and types in the class reference for it.

??? Subviews ???
If you don't know what subviews are or how they can come in handy, perhaps it's time to step away from the real coding and go learn the fundamentals of iOS Development.
 
Ouch !

Transitioning from books to a real project is been tough !!! but I appreciate all the understanding and help people are giving me. Frankly it's a second career for me, since my old one is almost dead and buried.

but I'm doing my best.

I'll try not to ask any more stupid questions. but it's a confusing maze of too much information.
thanks for any help
Ian
 
Didn't mean for that to hurt. It was just some advice before you get too far in over your head.

Transitioning from books to a real project is been tough !!!
I understand. But what books have you been learning from that don't teach you about subviews?

I'll try not to ask any more stupid questions.
It wasn't a stupid question. It's just that without a decent knowledge of the fundamentals, any answer given may not make any sense. Kind of like if you had asked how to write a book in French and I answered you "en francais". :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.