Re: actions on window open and close
Re: actions on window open and close
- Subject: Re: actions on window open and close
- From: Ricky Sharp <email@hidden>
- Date: Sun, 21 Jan 2007 08:57:28 -0600
On Jan 20, 2007, at 2:06 PM, Elie Zananiri wrote:
I have a subclass of NSWindowController that I want to perform
actions when the window becomes visible and to clean the variables
it is using when the window closes. I am having two problems with
this.
1) What methods can I use to know that a window is opening? I
looked at awakeFromNib and windowDidLoad, but both of these only
get called the first time the window opens. As for closing, I got
it to work with the windowWillClose notification. Is there a
windowDidOpen (or something like that)?
I think the issue with windowDidLoad only being called once is that
when using window controllers, the window will not be released when
closed (see docs for setReleasedWhenClosed:).
2) Everytime the window opens, I would like to generate a random
number and create an NSString using that number. This NSString
should be accessible to other class methods. I'm not sure how to
proceed, as I am a little confused with 'alloc' and 'init'. Can I
'alloc' and 'init' an object once and then re-init it everytime I
need it changed? Do I need to 'release' it first?
If you will only ever have a single window that is being opened/
closed, then I would create a singleton to manage your random-number
state. The code that needs the random number state will:
MyState* = [MyState sharedState]; // create if needed.
When you close your window, you then destroy the singleton. Even
though there's no 'window opened' notification to tap into, the next
time your code needs to access the random number state, you'll just
be calling your singleton object. Which, will create a new instance
of itself since it's once again nil.
If you must work with multiple windows, I would guess that you would
need to manage your windows more manually. i.e. be able to use the
released when closed process. I would then tie the random-number
generation to the window's initXXX method.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden