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