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 > I need help with this!

I need help with this!
Thread Tools
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Mar 10, 2001, 09:51 PM
 
Hey everyone,
I really want to get this to work. Thanks for any help. Controller is a delegate of myWindow and timeDisplay is linked to an NSTextfield. I want this to be like a clock, but when it runs(without any errors or warnings) it just displays the window and the textfield is unchanched from its default state.


#import <Cocoa/Cocoa.h>

@interface Controller : NSObject
{
IBOutlet id myWindow;
IBOutlet id timeDisplay;
NSTimer* timer;
}
- (void)dealloc;
- (void)windowDidLoad;
- (void)setTime:NSTimer;
@end

#import "Controller.h"

@implementation Controller

- (void)dealloc
{
[super dealloc];
[timer release];
//release stuff here
}

- (void)windowDidLoad
{
timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(setTime:sendingtimer userInfo:nil repeats:YES ] retain];
[timer fire];
//sets off timer when window loads
}

- (void)setTime:sendingtimer
{
NSDate *today = [NSDate date];
NSString *stoday = [today description];
[timeDisplay setStringValue:stoday];
}
@end



------------------
Think Different.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Mar 11, 2001, 05:22 AM
 

-windowDidLoad is an NSWindowController method; it's not a window delegate method. As your code stands, whichever method is loading the nib should be calling your windowDidLoad method explicitly to fire off the timer.

NSWindowController automatically loads the nib when its -window method is called for the first time, and calls -windowDidLoad on itself immediately afterwards. Thus, if your controller class was an NSWindowController subclass, you'd get more of the effect you wanted.

If this is the main nib (the one automatically loaded when the program starts up) then you probably want to fire off the timer from the application delegate method -applicationDidFinishLaunching: instead.

Secondly, the selector name with @selector() should not contain any the paramater names -- just the pieces that are the method name. In your case, @selector(setTime, as "sendingtimer" is the variable name (with the understood type of "id"). If the timer was ever actually fired off, you'd probably get a bunch of selector not recognized exceptions.

Following code is untested...

@interface Controller : NSWindowController
{
IBOutlet NSTextField *timeDisplay;
NSTimer *timer;
// superclass has window instance variable
}
- (void)setTimeNSTimer *)aTimer;
@end

@implementation Controller

- init
{
return [super initWithWindowNibName:@"MyNib"];
}

- (void)dealloc
{
[timer release]; // release our stuff *before* [super dealloc]
[super dealloc];
}

- (void)windowDidLoad
{
[super windowDidLoad]; // not strictly necessary but good form
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(setTime
userInfo:nil repeats:YES ] retain];
[timer fire];
}

- (void)setTimeNSTimer *)sendingtimer
{
NSString *stoday = [[NSDate date] description];
[timeDisplay setStringValue:stoday];
}

@end
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Mar 11, 2001, 01:00 PM
 
Thank you! This is why I love macnn, it has such a helpful community. Just one more question though - why didn't you use the applicationDidFinishLaunching method that you described, and why did you take out dealloc and windowDidLoad from the interface part?

Thanks again,
Dave

------------------
Think Different.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Mar 11, 2001, 01:19 PM
 
Unfortunately, it still doesn't work after a recompile with your code. I copied it exactly. Thanks for the help, but there is still no errors or warnings, but the window still just sits there in an unchanged form.
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Mar 11, 2001, 05:22 PM
 
why didn't you use the applicationDidFinishLaunching method that you described?
Ah. I assumed this was a separate .nib file, different than the app's main nib file. NSWindowController has responsibility for loading its nib; if your window is in the main application nib then don't use that approach, as NSApplicationMain() is responsible for loading the nib in that case. Simply use your original code and use a method something like the following:

Code:
- (void)applicationDidFinishLaunching:(NSNotification *)notification { timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(setTime:) userInfo:nil repeats:YES ] retain]; [timer fire]; }
Make sure to make the controller the delegate of the NSApplication object (and fix your -dealloc method too).

why did you take out dealloc and windowDidLoad from the interface part?
More of a personal preference. The @interface section is for the compiler's benefit; you don't strictly need to declare every method you implement in the .h file (you could declare them in the .m file, or if a method is only used by method implemented beneath it in the file or not explicitly called by other code then they technically don't need to be declared at all to make the compiler happy).

Still, it is good form to declare methods that are implemented in the .m file for clarity's sake. -dealloc is probably an exception, since it's such a common method with such a well-defined purpose that it really just clutters the .h file. It may be good to add -windowDidLoad to the .h file to indicate that the class is overriding the NSWindowController method. In both cases the methods are declared by the superclass anyways so the compiler doesn't care.

If you are writing library classes for use by other people, then you may want to be more careful of what's in the .h file since that usually connotes "public" API. I've probably picked up some bad habits by thinking in the "public API" mode too much :-)

Unfortunately, it still doesn't work after a recompile with your code.
Like I said, untested :-)

In any event, if your .nib is the main .nib, then an NSWindowController subclass is the wrong approach -- things could get ugly if it tries to unarchive another copy of the main nib.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Mar 11, 2001, 06:47 PM
 
I'll try this new code. Even if it doesn't work I just want you to know that I appreciate all the help. Thank you very much. It's people like you that inspire people like me.

Dave

------------------
Think Different.
     
davecom  (op)
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Mar 11, 2001, 06:53 PM
 
Vantastic! It works!
     
   
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:52 PM.
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