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对象传参的通用的形式。