| OS X - Development and Darwin Discussion and questions about development for Mac OS X. |
| Post Reply | New Thread | Subscribe |
|
|
Thread Tools |
![]() Member Since: Jun 10, 2011
Posts: 4
![]() |
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.. Last edited by NorthDakota91; 06-10-2011 at 11:22 AM. Reason: Wierd uploader behaviour |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
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; 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];
}
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 | |
![]() Member Since: Jun 10, 2011
Posts: 4
![]() |
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:
|
|||||
| QUOTE Thanks | ||||||
![]() Member Since: Jun 10, 2011
Posts: 4
![]() |
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 | |
![]() Member Since: Jun 10, 2011
Posts: 4
![]() |
). Now I can simply manage the button matrix from my code. Problem solved
Eventually, sorry for my bad english :S |
| QUOTE Thanks | |
![]() Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
![]() ![]() ![]() ![]() ![]() ![]() ![]() Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
|
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 |
| Thread Tools | |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|
|||||||
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