2011-07-22 74 views
0

我想了解如何將方法作爲參數傳遞,然後執行它們。在我的代碼中,我有一個方法,當單擊按鈕時調用get方法(最後一個方法)我試圖將method1和method2傳遞給方法placeSlide。我能夠做到這一點,但是當我嘗試執行它們時,我得到一個錯誤。我究竟做錯了什麼?作爲參數傳遞時無法執行方法iPhone/iPad

-(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

你會得到什麼錯誤? – ColdLogic

回答

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];