NSTimer and a problem with document-based apps
NSTimer and a problem with document-based apps
- Subject: NSTimer and a problem with document-based apps
- From: "Sumner Trammell" <email@hidden>
- Date: Sat, 26 Jul 2008 18:34:24 -0400
Hi. I'm writing a Cocoa document-based app (using the Xcode template) that
uses a WebView and needs an NSTimer to trigger one of the methods every 10
seconds in the MyDocument class. In my init method, I set up the timer:
@interface MyDocument : NSDocument
{
IBOutlet WebView *webView;
NSTimer *timer;
}
@implementation MyDocument
- (id)init {
self = [super init];
if (self) {
timer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(checkVPN:)
userInfo:nil
repeats:YES];
[timer retain];
}
return self
}
.
.
.
The timer runs fine and the web view gets initialized and displays webpages
properly. Everything is fine until you click a link in the web view, and
that particular link happens to open up a new window, which is now a new
instance of MyDocument.
That new instance of course calls init, and suddenly I have two NSTimers
running when I only wanted one timer for the whole app.
I'm not sure how to do what I want the right way. I think I need to
initialize the timer "further up," to put it lamely. Where should I do this?
In main()? Do I need to subclass something that NSDocument is a descendant
of? Can anyone point me ot examples of how to do this the right way?
I've read the NSRunLoop and NSTimer docs, but I'm still not sure how to fix
this.
Thanks.
-s
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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