天天看點

通過NSTimer看IPhone對@selector的函數如何傳參數

nsmutabledictionary *dict = [[nsmutabledictionary alloc] init];  

    if(oldview != nil)

    {

        [dict setobject:oldview forkey:@"oldview"]; 

    }

    if(newview != nil)

        [dict setobject:newview forkey:@"newview"]; 

    } 

    [nstimer scheduledtimerwithtimeinterval:0.0 target:self selector:@selector(ontimer:) userinfo:dict repeats:no];

    [dict release];

- (void)ontimer:(nstimer *)timer 

{  

    uiview *oldview = [[timer userinfo] objectforkey:@"oldview"];

    uiview *newview = [[timer userinfo] objectforkey:@"newview"];  

    [uiview animatewithduration:2.0  delay:0

                        options:uiviewanimationoptionallowuserinteraction

                     animations:^{  

                         oldview.alpha = 0.0; 

                         newview.alpha = 1.0;  

                     }  

從上可以看出,nstimer在對@selector(ontimer:)傳遞參數時,将傳參的對象儲存在了nstimer的userinfo的字典裡,在-

(void)ontimer:(nstimer *)timer中

通過取出該字典加以使用。

這個其實也就是iphone對@selector對象傳參的通用的形式。

繼續閱讀