actions on window open and close
actions on window open and close
- Subject: actions on window open and close
- From: Elie Zananiri <email@hidden>
- Date: Sat, 20 Jan 2007 15:06:52 -0500
Hello,
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)?
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?
Here is my code:
************************* MyController.h *************************
#import <Cocoa/Cocoa.h>
@interface MyController : NSWindowController {
NSString *myFile;
}
- (void)handleWindowClose:(NSNotification *)note;
@end
************************* MyController.m *************************
#import "MyController.h"
@implementation MyController
-(id)init
{
self = [super initWithWindowNibName:@"Movie"];
// seed random number generator
srandom(time(NULL));
// init the string
myFile = [[NSString alloc] init];
// register with the notification center
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleWindowClose:)
name:@"NSWindowWillCloseNotification"
object:nil];
return self;
}
// THIS SHOULD BE CALLED EVERYTIME THE WINDOW IS OPENED
- (void)windowDidLoad
{
NSLog(@"opening the window");
// generate a random number
int randNumber = random()%3+1;
// construct the filename
[myFile initWithString:[[[NSNumber numberWithInt:randNumber]
stringValue] stringByAppendingString:@".mov"]];
}
- (void)handleWindowClose:(NSNotification *)note
{
NSLog(@"closing the window");
// [myFile release]; should I do this and re-alloc each time?
}
- (void)dealloc
{
// unregister with the notification center
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
[super dealloc];
}
@end
********************************************************************
Thanks for the help,
Elie Zananiri
_______________________________________________
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