NSTimer delegate selection

I am trying to create a structure for all the custom objects and views that I created and often use, creating custom delegate classes and custom objects. Everything went well, except for trying to force NSTimers to call the correct method inside the delegate class.

Here is the basic setup.

-(void) startTimers {
    NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(doSomething:) userInfo:nil repeats:YES];
    NSTimer *timer2 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(doSomethingElse:) userInfo:nil repeats:YES];
}

I just can just call this method and whatever, but when this time fires, it does not call the method that I defined as a selector. I am sure that this has something to do with the value of the delegate and the class that it does as a delegate.

Please note that the file I'm writing is a subclass of UIView that is configured as a delegate using @protocol tags and all that.

What should I set as a goal when defining my timers in order to get them to call the correct methods.

EDIT:

Here is an example of what I am doing:

ExampleView.h

#import <UIKit/UIKit.h>

@protocol ExampleViewDelegate;

@interface ExampleView : UIView {
    NSTimer *timer;
}
-(void) initWithStuff:(id)stuff andFrame:(CGRect)frame;
-(void) testTimer;
@end

@protocol ExampleViewDelegate

-(void) someDelegateFunction;
@end

ExampleView.m

#import "ExampleView.h"

@implementation ExampleView


-(id) initWithStuff:(id)stuff andFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(testTimer) userInfo:nil repeats:YES];
    return self;
}

-(void) testTimer {
    NSLog(@"Timer Fired");
}

@end

If you add this custom view to the view manager, it will never call this function testTimer and will not print “Timer Fired”. So I think that when I set the delegate for this timer, it actually sets it for something else. Any ideas?

0
source share
1 answer
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(doSomething:) userInfo:nil repeats:YES];

Note that the method is called "schedTimerWithTimeInterval"

+2
source

All Articles