View Full Version : Newbie question on NSView
Keumar
2004.08.11, 04:51 PM
Hello everyone!
I'm new to coding on Macs, and I have a question about NSView:
When making a subclass of NSView, how do we access it from another class, say I have a MyView (Custom NSView) and a MyCtrl (NSObject). I want to do stuff in MyCtrl and call "setNeedsDisplay:YES" on the NSView, but since MyView is not something i can instantiate in MyCtrl how do I set the receiver of the method?
Also, along the same lines, how would I set up someway for both MyView and MyCtrl to access a data class (for ex: MyCtrl modifies some data that after a "setNeedsDisplay" MyView checks to draw according to the new data)
Hope this makes some sense :)
Thanks!
-Marc
AndrewSage
2004.08.12, 03:11 AM
Welcome to Mac coding - I hope you enjoy your stay!
May I suggest you take a look at the FlipSquare (http://www.createmacgames.org/index.php?option=content&task=category§ionid=3&id=17&Itemid=27)
tutorials that I wrote as they explain how to do what you are trying to do. It is easier for you to read them then me to try and explain in just words here.
I hope that helps.
JeremyL
2004.08.12, 06:09 AM
When making a subclass of NSView, how do we access it from another class, say I have a MyView (Custom NSView) and a MyCtrl (NSObject). I want to do stuff in MyCtrl and call "setNeedsDisplay:YES" on the NSView, but since MyView is not something i can instantiate in MyCtrl how do I set the receiver of the method?
One option is to add an IBOutlet to MyCtrl's interface, e,g
@interface MyCtrl : NSObject {
IBOutlet id instanceOfMyView;
...
Then use Interface Builder to connect the outlet to instanceOfMyView, and call [instanceOfMyView setNeedsDisplay:YES] from MyCtrl.
In general, you can access functions from other classes by using the NSNotificationCenter system. Use addObserver:selector:name:object: to allow an object's function (selector) to receive notifications, then use postNotificationName:object: to execute the selector from any class.
JeremyL
2004.08.12, 07:03 AM
Also, along the same lines, how would I set up someway for both MyView and MyCtrl to access a data class (for ex: MyCtrl modifies some data that after a "setNeedsDisplay" MyView checks to draw according to the new data)
This should work: add pointers to an instance of your data class to the interfaces of MyView and MyCtrl, then add a setData:(MyDataClass)dataStore selector to MyView. Use a MyCtrl selector to a) create and initialize the instance of the data class and b) call MyView's setData selector to set MyView's pointer to your newly created data object.
MyCtrl and MyView point to the same data object, so you can access and modify your data from either class.
Keumar
2004.08.12, 10:51 AM
:D Thanks guys! This should help me alot.
-Marc
Keumar
2004.08.19, 12:21 PM
Well BeyondCloister's tutorial and this website have been a huge help, over the weekend i wrote a little tetris clone, was alot of fun to do. I have the game working, but I'd like to add a "start" and "game over" screen (right now its just a red dot in nsview for start and a blue one for game over :) ), I've tried to display an image in the nsview from what i've read in the forum, but nothing shows up :???:
I'm at work so i don't have the code with me, but this is what i do in drawrect:
I load the image that i have in my resources to a NSImage
call compositeToPoint: NSMakePoint(x,y):operation:NSCompositeCopy
But nothing.
anyone know what I'm missing? (sorry I know it would be ez'er with code here)
Thanks!
-Marc
Oh and my little app can be found here: blocks.dmg (http://www.tc.umn.edu/~beha0005/projects/Blocks.dmg)
(at red dot, just press 'return' to start, there might be some collision problems, I seem to find new ones every time i play it :wacko: )
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.