PDA

View Full Version : NSView and superview


Jake
2003.12.03, 05:50 PM
Ok... I have had this working before in another instance, but it won't work now. Here is my controller.m code (or part of it)

gameView = [ [ glView alloc ] initWithFrame:[ glWindow frame ] colorBits:16 depthBits:34 fullscreen:FALSE ];
[self addSubview:gameView];

Here is the problematic code in glView.m

if (inputUp == 0 ) {
controller* gv = (controller*)[self superview];

NSLog(@"1");
[currentLevel release];
NSLog(@"2");
levelLoaded = FALSE;
NSLog(@"3");
[golfer release];
NSLog(@"4");

if (mainWindow == nil) {
NSLog(@"4.2");
if (![NSBundle loadNibNamed:@"mainMenu.nib" owner:gv] ) {
NSLog(@"Load of mainMenu.nib failed");
}
}

[gv restartMainMenu];

NSLog(@"4.5");
[mainWindow makeKeyAndOrderFront: nil];
NSLog(@"5");
[glWindow close];
NSLog(@"6");

gv->inGame = FALSE;
[self release];


Here is the console...

2003-12-03 18:35:11.931 GL Golf[1131] 1
2003-12-03 18:35:11.956 GL Golf[1131] 2
2003-12-03 18:35:11.971 GL Golf[1131] 3
2003-12-03 18:35:11.985 GL Golf[1131] 4
2003-12-03 18:35:12.003 GL Golf[1131] 4.2
2003-12-03 18:35:12.054 GL Golf[1131] Could not connect the action terminate: to target of class NSGrayFrame
2003-12-03 18:35:12.073 GL Golf[1131] Could not connect the action orderFrontStandardAboutPanel: to target of class NSGrayFrame
2003-12-03 18:35:12.090 GL Golf[1131] Could not connect the action hideOtherApplications: to target of class NSGrayFrame
2003-12-03 18:35:12.107 GL Golf[1131] Could not connect the action hide: to target of class NSGrayFrame
2003-12-03 18:35:12.121 GL Golf[1131] Could not connect the action unhideAllApplications: to target of class NSGrayFrame
2003-12-03 18:35:12.437 GL Golf[1131] *** -[NSGrayFrame restartMainMenu]: selector not recognized
2003-12-03 18:35:12.448 GL Golf[1131] *** -[NSGrayFrame restartMainMenu]: selector not recognized

glGolf has exited due to signal 11 (SIGSEGV).


Hum... any ideas?

Criollo
2003.12.04, 02:23 AM
Don't know what you're trying to do here, but I think this line of code might be the problem:

controller* gv = (controller*)[self superview];

The [self superview] method is just going to return glView's superview, whatever that is(probably NSGrayFrame), and won't give you access to your controller's gameView variable. I think what you want to do is instantiate your controller class in Interface Builder and give your glView an outlet that connects to the controller.

-Criollo

Sohta
2003.12.04, 08:27 AM
The [self superview] method is just going to return glView's superview, whatever that is(probably NSGrayFrame), and won't give you access to your controller's gameView variable

That is incorect, in Obj-c, ALL class instances are instances of the id class. Using pointers to define the class of the object is just a helper so that the compiler can give you warnings (not even errors) about unsuported methods. All instances have the isa member that points to the members class...

GoodDoug
2003.12.04, 12:41 PM
Originally posted by Sohta
That is incorect, in Obj-c, ALL class instances are instances of the id class. Using pointers to define the class of the object is just a helper so that the compiler can give you warnings (not even errors) about unsuported methods. All instances have the isa member that points to the members class...

That's not germane to the discussion... while what you said is technically correct, it has nothing to do with what Criollo wrote.

Criollo is correct, making assumptions on the view hierarchy like that will usually get you into trouble. You need to find another way to get a pointer to your controller object. Either find a way to do it in your nib and make a connection, or explicitly set it in code.

Jake
2003.12.04, 02:57 PM
Are you confusing superview with just super (or am i)? I know super will return the superclass (for instance NSOpenGLViews super is NSView), but doesn't superview return the object that made it? For instance my level is a subview of NSObject, but I still use the same superview method to return the object that created it (glView)...

Anyways, I can just add an ID variable called maker to my init method in glView I think.

Johan
2003.12.04, 04:45 PM
A little side note...

gameView = [ [ glView alloc ] initWithFrame:[ glWindow frame ] colorBits:16 depthBits:34 fullscreen:FALSE ];
[self addSubview:gameView]
depthBits should probably be 32, not 34.

.johan

Jake
2003.12.04, 04:56 PM
I fixed the problem, but I am still a little confused on superview...


Originally posted by Johan
A little side note...

gameView = [ [ glView alloc ] initWithFrame:[ glWindow frame ] colorBits:16 depthBits:34 fullscreen:FALSE ];
[self addSubview:gameView]
depthBits should probably be 32, not 34.

.johan

Thanks!

Criollo
2003.12.04, 06:06 PM
Views are organized into a hierarchy. At the top is your window's content view, and all your other views reside inside there. Say you were to create a custom view in IB, drag it onto a window, and then drag a button into the custom view. The custom view's superview is the window's content view, and its subview is the button. The button's superview is the custom view, and its subview is nil. So basically, none of this is going to gain you access to your controller.

I would recommend just instantiating your controller and hooking up connection in IB. For an example of this, see how Hooptie (http://sourceforge.net/projects/inkubator/) handles its controllers.

-Criollo

Codemattic
2003.12.04, 08:12 PM
Originally posted by Jake
I fixed the problem, but I am still a little confused on superview...

and the solution was...?

Jake
2003.12.09, 07:31 AM
Sorry, I forgot about this thread. The solution was to make my drawRect method a BOOL instead of VOID, so if I returned TRUE I would do something special, otherwise FALSE meant it was just a normal frame.