NSView mouseDown: Issue

Discussion in 'Mac Programming' started by Blakeasd, May 22, 2011.

  1. macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am having some issues with the mouseDown method.

    Here is my code:

    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!
     
  2. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
  3. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I'm not sure what you mean by 'nil'
    I declare the window like so...
    Code:
    IBOutlet NSWindow *window;
    
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    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:
    Code:
    NSLog(@"window=%@", window);
    
     
  5. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #5
    It prints the following:

    News Tiles[9989:60b]
    window=(null)
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    It looks like 'window' isn't connected in the nib. Double-check your connections.
     
  7. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #7
    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?
     
  8. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    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:
    Code:
    - (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.
    Code:
      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.
     
  9. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    The following is outputted:


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

    Sydde

    Joined:
    Aug 17, 2009
    #10
    What does this output:

    Code:
    NSLog( @"%@", [self window] );
    when you put it in one of those methods?
     
  11. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #11
    @Sydde The following gets printed out:
    2011-05-24 18:13:56.853 News Tiles[4265:60b] <NSWindow: 0x1005291a0>
     
  12. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #12
    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?
     
  13. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #13
    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.
     
  14. thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #14
    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
    Code:
    
     [ [self window] setTitle:@"News Tiles"];
     [ [self window]setContentView:tileView];
    
    
    Thanks Again!
     

Share This Page