performSelector:@selector withObject:nil afterDelay:not calls方法(Objective C)

时间:2015-06-16 13:25:05

标签: objective-c performselector

我尝试在早上通过委托实现警报和人员关系,并模拟每个环使用后的延迟performSelector:@selector withObject:nil afterDelay。但是没有执行函数调用,但是当我只使用performSelector: - 它工作正常。全部在主线程中。有人可以解释一下吗?这是我的代码:

    #import <Foundation/Foundation.h>

    @protocol MyDelegateProtocol <NSObject>

    @optional
    - (void) wakeUp;
    - (void) standUp;

    @end

#import <Foundation/Foundation.h>
#import "myDelegateProtocol.h"

@interface Alarm : NSObject

@property (nonatomic, weak) id <MyDelegateProtocol> delegate;
@property (nonatomic, assign) NSUInteger ringsLeft;
@property (nonatomic, assign) SEL selector;

- (void) setAlarm: (CGFloat) delay;
- (void) fiveMinutesMore;
- (id) initWithRings: (NSUInteger) rings;
- (void) doAlarm;


@end

#import "Alarm.h"

@implementation Alarm

@synthesize delegate;
@synthesize selector;

- (id) initWithRings:(NSUInteger)rings
{
    self = [super init];
    if (self) {
        self.ringsLeft = rings;
    }
    return self;
}
- (void)setAlarm:(CGFloat)delay {
    if (delegate) {
        if (self.ringsLeft>0) {
            self.ringsLeft--;
            selector = @selector(wakeUp);
        } else {
            selector = @selector(standUp);
        }
    }
    if ([delegate respondsToSelector:selector]) {
        [self performSelector:@selector(doAlarm)];
//        [self performSelector:@selector(doAlarm) withObject:nil afterDelay:delay];
    }
}
- (void)doAlarm {
    [delegate performSelector:selector];
}
- (void)fiveMinutesMore {
    [self setAlarm:1.0];
}

@end

#import <Foundation/Foundation.h>
#import "Alarm.h"

@interface Person : NSObject <MyDelegateProtocol>

@property (nonatomic,assign) NSUInteger chanceToWakeUp;
@property (nonatomic,strong) Alarm *myAlarm;

- (void) goSleepFor: (CGFloat) seconds;
- (id) init;

@end

#import "Person.h"

@implementation Person

@synthesize chanceToWakeUp;
@synthesize myAlarm;

-(void) goSleepFor:(CGFloat)seconds
{
    NSLog(@"Going for a sleep");
    [myAlarm setAlarm:seconds];
}
-(void) wakeUp
{
    NSLog(@"Ringing...");
    NSUInteger randomNumber = arc4random_uniform(6);
    if (randomNumber>chanceToWakeUp) {
        chanceToWakeUp++;
        NSLog(@"Person takes five minutes more to sleep");
        [myAlarm fiveMinutesMore];
    } else {
        [self standUp];
    }
}
-(void) standUp
{
    NSLog(@"Person stand's up");
}
-(id) init
{
    self = [super init];
    if (self) {
        chanceToWakeUp=0;
        myAlarm = [[Alarm alloc] initWithRings:5];
        myAlarm.delegate=self;
    }
    return self;
}

@end

#import <Foundation/Foundation.h>
#import "Person.h"


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Person *tiredPerson = [[Person alloc] init];
        [tiredPerson goSleepFor:10.0f];
    }
    return 0;
}

4 个答案:

答案 0 :(得分:0)

使用:

[self performSelector:@selector(doAlarm:) withObject:nil afterDelay:delay];

必须定义该方法以获取参数:

- (void)doAlarm:(id)object {

[self performSelector:@selector(doAlarm) withObject:nil afterDelay:delay];

必须定义该方法以获取参数:

- (void)doAlarm {

答案 1 :(得分:0)

我认为您的计划在延迟时间之前终止。尝试添加:

unset($doc->_styleSheets[$this->baseurl.'/path/to/some.css']);
[[NSRunLoop currentRunLoop] run] 来电后

答案 2 :(得分:0)

执行带延迟的选择器会对当前运行循环的操作进行排队。你没有在你的程序中启动任何运行循环。你的程序只是同步调用一些函数返回然后程序退出。

答案 3 :(得分:0)

如果您的代码在运行循环处于默认模式以外的其他模式时正在运行,请尝试以下操作:

        [self performSelector:@selector(doAlarm) withObject:nil
               afterDelay:delay
                  inModes:@[[[NSRunLoop currentRunLoop] currentMode]]];