一个类调用另一个类的方法(nstimer),无法调用选择器

时间:2015-02-21 12:54:30

标签: ios objective-c

我有一个视图,当加载实例化另一个使用nstimer的类中的方法时。然后,nstimer调用另一个方法来填充将用于填充表视图的数组。但是,nstimer选择器返回'终止应用由于未捕获的异常' NSInvalidArgumentException',原因:' + [inbox repliesMethod]:无法识别的选择器发送到类0x102a199d8'&#39 ;。出于我对这一个的深度。我检查了选择器的名称,它是正确的,第一个视图确实调用了计时器,但在6秒后发生了崩溃。感谢

第一个视图

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

     thankYouMessage.text = [NSString stringWithFormat:@"Thank You %@ for your message. Your message has reached Hubble. We will reply shortly.", self.name3];

    self.navigationItem.hidesBackButton = YES;
    self.messageSent = @"yes";

    NSLog(@"Call timer method");
    [inbox timerForOtherMethods];
}

第二个视图(收件箱)

+(void)timerForOtherMethods {

    NSLog(@"populate array timer");
NSTimer *timer = [NSTimer timerWithTimeInterval:6 target:self selector:@selector(repliesMethod) userInfo:nil repeats:NO];
NSLog(@" add timer");
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

}

-(NSMutableArray *)repliesMethod {
    NSLog(@"Create replies array");
    NSMutableArray *replies = [[NSMutableArray alloc]init];

    NSLog(@"Create dictionary reply");
    NSDictionary *reply = [NSDictionary dictionaryWithObjectsAndKeys:@"From:", @"The Sorting Office", @"Subject:", @"Message from Hubble", nil];

    NSLog(@"add reply to replies array");
    [replies addObject:reply];

    NSLog(@"Add reply to all replies");
    self.allReplies = replies;

    NSLog(@"%@", allReplies);

    return replies;

}

2 个答案:

答案 0 :(得分:0)

timerForOtherMethods是一种类方法。当你在类方法中说self时,它意味着类而不是类的实例。这意味着您的计时器正在尝试调用+repliesMethod而不是-repliesMethod

答案 1 :(得分:0)

您需要更改:

+(void)timerForOtherMethods

为:

-(void)timerForOtherMethods
相关问题