PDA

View Full Version : NSView mouseDown: Issue




Blakeasd
May 22, 2011, 05:55 PM
Hello,
I am having some issues with the mouseDown method.

Here is my code:


-(void)mouseDown:(NSEvent *)theEvent{

[window setTitle:@"News Tiles"];
[window setContentView:tileView];



}

This code is inside WelcomeView.h (Subclass of NSView). window is an IBOutlet NSWindow and tileView is an IBOutlet NSView.
Why won't the window's tile change and why won't the view change. I get no errors or warnings the code just doesn't work. I put an NSLog inside the mouseDown: method to see if WelcomeView was taking any clicks and it was. The NSLog worked, but the setTitle and setContentView don't
Any Ideas?
Thanks!



jiminaus
May 22, 2011, 06:06 PM
Is window nil?

Blakeasd
May 22, 2011, 06:58 PM
I'm not sure what you mean by 'nil'
I declare the window like so...

IBOutlet NSWindow *window;

jiminaus
May 22, 2011, 09:35 PM
I'm not sure what you mean by 'nil'
I declare the window like so...

IBOutlet NSWindow *window;


nil = a pointer variable that's not pointing to any object.

Insert the following code at the top of the method and tell us what it prints:

NSLog(@"window=%@", window);

Blakeasd
May 23, 2011, 01:44 PM
It prints the following:

News Tiles[9989:60b]
window=(null)

kainjow
May 23, 2011, 05:43 PM
It looks like 'window' isn't connected in the nib. Double-check your connections.

Blakeasd
May 23, 2011, 06:17 PM
It is connected. It is connected to the AppDelegate AND to a WelcomeView. So can you have it connected to 2 .h files. Can you not do that?

jiminaus
May 23, 2011, 10:08 PM
It is connected. It is connected to the AppDelegate AND to a WelcomeView. So can you have it connected to 2 .h files. Can you not do that?

You can have an object connected to as many outlets as your want. What you can't do is try to connect multiple objects to a single outlet.

Try adding the following code to your WelcomeView:

- (void)setWindow:(NSWindow *)aWindow
{
NSLog(@"%s self=%@ aWindow=%@", __PRETTY_FUNCTION__, self, aWindow);
window = aWindow;
}


And then put the following to code at the top of mouseDown.

NSLog(@"%s self=%@ window=%@", __PRETTY_FUNCTION__, self, window);


What I'm looking for is a) is the window being set, and b) is the window being set in the same instance of WelcomeView as is being called by mouseDown. So I'd hope to see 2 log messages, both with the same self and window.

Blakeasd
May 24, 2011, 01:46 PM
The following is outputted:


-[WelcomeView mouseDown:] self=<WelcomeView: 0x100536eb0> window=(null)

Sydde
May 24, 2011, 04:25 PM
What does this output:

NSLog( @"%@", [self window] );

when you put it in one of those methods?

Blakeasd
May 24, 2011, 05:15 PM
@Sydde The following gets printed out:
2011-05-24 18:13:56.853 News Tiles[4265:60b] <NSWindow: 0x1005291a0>

Sydde
May 24, 2011, 05:27 PM
@Sydde The following gets printed out:
2011-05-24 18:13:56.853 News Tiles[4265:60b] <NSWindow: 0x1005291a0>

Is that the window that contains your view, or are you concerned about a different window? Because a view almost always knows what window it is in. Would using the -window method be good enough?

jiminaus
May 24, 2011, 06:05 PM
Is that the window that contains your view, or are you concerned about a different window? Because a view almost always knows what window it is in. Would using the -window method be good enough?

Great suggestion.


@Blakeasd Go with Sydde's suggestion.

If this is a different window, then you should name your ivar something different so to avoid any potential confusion with window as inherited from NSView.

But that doesn't explain why you didn't get a log message from setWindow. There is something wrong with your NIB connection between the window and the view.

Blakeasd
May 24, 2011, 06:19 PM
Thanks @Sydde and @jiminaus. I used the views -window method and it worked.
Here is my fixed code if anyone has a similar problem:
This is in my mouseDown: method inside the WelcomeView


[ [self window] setTitle:@"News Tiles"];
[ [self window]setContentView:tileView];


Thanks Again!