OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Managing a button group in Cocoa


Post Reply New Thread Subscribe

 
Thread Tools
NorthDakota91

 
Member Since: Jun 10, 2011
Posts: 4
NorthDakota91 is on a distinguished road

NorthDakota91 is offline
Hi all, this is my first post! I've started learning obj-c/cocoa for mac few days ago and I've decided to port a small project in Qt (a minefield game) to Mac/Cocoa.

I have some problems in writing the controller. I have to manage a group of buttons that represent my minefield. I have no problems with actions: from what i understand i just have to link a button to the action from the interface builder and then simply copy/paste the resulting button.

But how can I manage the outlets? In Qt there is an object called QButtonGroup that lets me create a matrix of buttons and simply link them to the code. Is there something similar in Cocoa/ObjC? Can someone kindly point me in the right direction?


I've attached the project, just in case..
Attached Files
File Type: zip Minefield.zip (71.2 KB, 1 views)

Last edited by NorthDakota91; 06-10-2011 at 11:22 AM. Reason: Wierd uploader behaviour
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
I've done something similar. Maybe this will be helpful to you.

I didn't use buttons. Instead I used NSImageView, NSImage and NSView. I subclassed NSImageView to hold and handle NSImages I was flipping between. Each square in a grid was an instance of my NSImageView subclass. When clicked on, captured via the mouseDown event, I called a delegate which is my game engine. My engine was responsible for a few things including messaging back to the square to perform an appropriate action.

In my game, the grid was dynamically created. I had a background view which is a fixed size but the active squares on it could be 7 or 8 across by 8 or 9 down. That background view was used to group the squares. This was a port of something I'm doing on iOS so more squares didn't make sense. The dynamic creation means that I did not use Interface Builder to build the grid of squares, and I could create any size grid. You can change the size of the grouping view dynamically if needed. I placed that in IB as a starting representation and linked it to an outlet for possible action on it.

Since my NSImageView is added as a subview to the group view, there is no need to keep track of them in an NSArray. The group view effectively acts as an array. Also, because of the dynamic nature, I don't need outlet ivars.

If you need to know which square has been clicked on, then perhaps use the tag property. You would set that as you create each square. Read the documentation on how to do that. You can set those in IB if you build your squares in it.

When you setup a button (or other interface type) to call an action method, you should be using a method that accepts the sender id. When the action is called, you can use that id to pass messages back to the sender. That method pattern would look like this;

Code:
- (IBAction) myAction: (id) sender;
So you could do have like this;
Code:
- (IBAction) myAction: (id) sender;
{
   NSInteger tag = [sender tag]; // tag should be unique to id specific sender
   if (tag == 0) [sender doSomething];
   else if (tag == 99) [sender doSomethingElse];
}
P.S. I haven't looked at your code. I tend not to download whole projects for questions. When you have something very specific then you should post the snippet of code.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
NorthDakota91

 
Member Since: Jun 10, 2011
Posts: 4
NorthDakota91 is on a distinguished road

NorthDakota91 is offline
Well, first of all - thank you for the reply

I suppose that I didn't explain my problem well though. What I want to do is change the image (or the label, whatever) of my matrix of buttons. So:

- The player clicks a button
- The controller gets the button's tag and transforms it in x and y coordinates
- The controller calls a recursive method of the model, passing x and y, where the cells are "uncovered"
- The controller now should write the number of bombs on the covered buttons. My problem is: how can I keep track of the buttons? From what I've understood, the only way to achieve this is to generate the matrix from the code. Or, perhaps, is there a way to ask buttons something like "who has this tag?"

To be honest I'm a little bit confised :S

Quote:
P.S. I haven't looked at your code. I tend not to download whole projects for questions. When you have something very specific then you should post the snippet of code.
Thank you for the advice
QUOTE Thanks
NorthDakota91

 
Member Since: Jun 10, 2011
Posts: 4
NorthDakota91 is on a distinguished road

NorthDakota91 is offline
It seems that I've found a partial solution - I've selected a button in IB, then I've selected Layout -> Embed Objects In -> Matrix and then I've just option-dragged the resize grip of the button to create my matrix. However, I can't understand how to control it from the controller

Eventually, sorry for my bad english :S
QUOTE Thanks
NorthDakota91

 
Member Since: Jun 10, 2011
Posts: 4
NorthDakota91 is on a distinguished road

NorthDakota91 is offline
Ok I figured it out - I just had to declare an NSMatrix* in the controller and drag connections from and to the matrix (that was easy enough ). Now I can simply manage the button matrix from my code. Problem solved

Eventually, sorry for my bad english :S
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Good to hear you found a better solution. I had forgotten that NSMatrix existed.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« Can you tell me which part of the code i can change the colour !!!!! | Terminal Help »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
MacBook Trackpad button (& left mouse button) not working Die Fledermaus Apple Notebooks 29 02-23-2013 10:01 AM
Home button on iPhone has a mind of it's own bailey.hipson iPhone Hardware and Accessories 1 02-14-2010 04:10 PM

All times are GMT -4. The time now is 02:43 PM.

Powered by vBulletin
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?