Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Progromattically adding NSViews...

Progromattically adding NSViews...
Thread Tools
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 10, 2001, 07:55 PM
 
For a my brushedMetalNSWindow framework, I'm attempting to progromatically add NSViews to an NSWindow declared in interface builder. Unfortunately, the progromatically generated NSViews aren't showing up on the screen at all. (after over 20 hours of bashing my head I'm gonna crack!

Are there any good examples of adding NSviews progromatically?
I think I've downloaded all the Obj-C / Cocoa / OS X dev / OpenStep references in existance yet to no avail.

I've pasted some of the code below...

<font face = "courier">[theView display];</font>
--&gt; properly invokes the custom NSView's drawRect routine

<font face = "courier">[theView setNeedsDisplay:YES];</font>
--&gt; doesn't do anything

The answer to this odity would probably explain alot.


Here's the custom class of NSWindow's initWithContentRect:... routine that I'm using to add additional custom NSViews

<font face = "courier">
- (id)initWithContentRect NSRect)contentRect styleMask unsigned int)aStyle backing NSBackingStoreType)bufferingType defer BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self)
{
theWindowDraggingView = [[WindowDraggingView alloc]
initWithFrame:NSMakeRect(0, 0, contentRect.size.width, contentRect.size.height)];
[theWindowDraggingView retain];
...
</font>
     
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status: Offline
Reply With Quote
Nov 11, 2001, 12:37 AM
 
Two things.. 1) Are you adding your view to the window? That seems pretty obvious but it's not in your code, so I don't know. See -[NSWindow setContentView:] and 2) You don't need to retain theWindowDraggingView if you use alloc to create it (which you are)

Matt
     
dfiler  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 11, 2001, 02:33 PM
 
Originally posted by mreda:
<STRONG>Two things.. 1) Are you adding your view to the window? That seems pretty obvious but it's not in your code, so I don't know. See -[NSWindow setContentView:] and 2) You don't need to retain theWindowDraggingView if you use alloc to create it (which you are)

Matt</STRONG>
Thanks for the reply...
(1) I'm definately using [NSWindow setContentView:] (see following posts that I origonally posted to the Apple OS X dev board.
(2) Yep, retain isn't needed. Thanks for the reminder. I've rewritten the code so many times now that I'm starting to purposely add things which, according to the documentation, aren't needed.

Maybe the following two posts will clarify the mystery
     
dfiler  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 11, 2001, 02:38 PM
 
I just noticed some other odities:

If I set the content view of a window to a custom NSView, the custom view's drawRect funtion is never called even via [[NSWindow contentView] display].

However, if I set the contentView to [[NSView alloc] initWithFrame:someRect], and then add the custom NSView as a subview of the contentView, the custom view's drawRect function is called when invoking [[NSWindow contentView] display].

This is kind of a pain since its just an extra NSView with no subviews except for the one that I actually want to be the windows contentView.

Yet either way the view don't draw themselves... ? I've checked the coordinates, and verified the custom view's superview and window. Yet even filling the view's frame with blackColor doesn't have any effect.
     
dfiler  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 11, 2001, 03:13 PM
 
Even more details:

While in the custom NSView's drawRect routine...

Drawing into or filling the view's visibleRect doesn't have any visible results.

I've verified the visible rect at various points during execution... It seems as if the drawing routines are drawing into views that have a visibleRect and exist in the view hierarchy.

[[theWindow contentView] display] indeed invokes the correct drawRect routines yet nothing shows up.
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Nov 11, 2001, 05:35 PM
 
Originally posted by dfiler:
<STRONG>[[theWindow contentView] display]</STRONG>
have you tried [[theWindow contentView] setNeedsDisplay:YES] ?
just a guess...

[edit: just noticed you have tried that -- never mind]

[ 11-11-2001: Message edited by: seb2 ]
     
dfiler  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 12, 2001, 12:25 AM
 
Originally posted by seb2:
<STRONG>

have you tried [[theWindow contentView] setNeedsDisplay:YES] ?
just a guess...

[edit: just noticed you have tried that -- never mind]

[ 11-11-2001: Message edited by: seb2 ]</STRONG>
I think this is one of the important clues here...

<font face = "courier">[[theWindow contentView] setNeedsDisplay:YES]</font>
-- never results in drawRect being called

<font face = "courier">[[theWindow contentView] display]</font>
-- properly calls drawRect since its designed to do it 'now' instead of when the window server gets around to it.

This caused me to investigate the view's visibleRect, figuring that if the window server doesn't want to call drawRect, it might be because none of the view is visible. The visibleRect checked out OK as far as I could tell.
     
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status: Offline
Reply With Quote
Nov 12, 2001, 02:24 PM
 
I don't really have anything else to offer off the top of my head, but if you want to email the code to me, I would be willing to take a look at it.

Matt
     
dfiler  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Pittsburgh
Status: Offline
Reply With Quote
Nov 13, 2001, 12:27 AM
 
Originally posted by mreda:
<STRONG>I don't really have anything else to offer off the top of my head, but if you want to email the code to me, I would be willing to take a look at it.

Matt</STRONG>
Thanks, i'll do that in the morning when I have time to clean up my messy code...

However, I seem to have made some progress!

In my custom window's init methods, I make sure to immediately call the super's init and then to work with the NSWindow it returns. Turns out, you can't add views to the NSWindow until after its subclasses have returned the newly inited customNSWindow.

I wish there was better documentation! I've read everything the included developer docs and Learning Cocoa have to say about these topics. Guess I'll read em another 30 times

Am I correct in pursueing the following tact?
I intend to subclass NSWindowController and use its windowDidLoad: method as the proper location to add NSViews to a newly initialized window. Is there a way to do this from within NSWindow without needing a window controller?

Maybe I should check to see if this actually works before elaborating

I only assume that it'll work since adding the NSViews from within &lt;font face = "courier"&gt;[customNSWindow mouseDown:]&lt;/font&gt; worked!

[ 11-13-2001: Message edited by: dfiler ]
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Nov 15, 2001, 04:48 PM
 
Just out of curiosity, what happens if you define the window programmtically and then change the NSView's programmtically?

F-bacher
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:51 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2