• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
actions on window open and close
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: actions on window open and close
      • From: Ricky Sharp <email@hidden>
    • Re: actions on window open and close
      • From: Jerry Krinock <email@hidden>
  • Prev by Date: Re: [NSApp mainMenu] = null!!!
  • Next by Date: Re: [NSApp mainMenu] = null!!!
  • Previous by thread: Re: Regarding launching an application at startup of mac.
  • Next by thread: Re: actions on window open and close
  • Index(es):
    • Date
    • Thread