作为参数iPhone / iPad传递时无法执行方法

时间:2011-07-22 16:55:24

标签: iphone objective-c ipad pointers

我正在尝试学习如何将方法作为参数传递,然后再执行它们。在我的代码中,我有一个方法,在单击按钮时调用(最后一个方法)我试图将method1和method2传递给方法placeSlide。我能够这样做,但当我尝试执行它们时,我得到一个错误。我究竟做错了什么?

-(void) method1{

}
-(void) method2{

}

-(void) placeSlide: (SEL) meth1 OtherMethod: (SEL) meth2{




    NSTimer *timer1;
    timer1 = [NSTimer scheduledTimerWithTimeInterval: (1)
                                             target: self
                                           selector: @selector(meth1)
                                           userInfo: nil
                                            repeats: NO];
    NSTimer *timer2;
    timer2 = [NSTimer scheduledTimerWithTimeInterval: (1.2)
                                             target: self
                                           selector: @selector(meth2)
                                           userInfo: nil
                                            repeats: NO];
}

// get's executed when I click on a btn
-(IBAction) ButtonClick{
    [self placeSlide:@selector(method1) OtherMethod:@selector(method2)];
}

我得到的错误是: enter image description here

1 个答案:

答案 0 :(得分:3)

按原样传递SEL个变量。

NSTimer *timer1;
timer1 = [NSTimer scheduledTimerWithTimeInterval: (1)
                                         target: self
                                       selector: meth1
                                       userInfo: nil
                                        repeats: NO];
NSTimer *timer2;
timer2 = [NSTimer scheduledTimerWithTimeInterval: (1.2)
                                         target: self
                                       selector: meth2
                                       userInfo: nil
                                        repeats: NO];