OC中@Selector传参总结

一、一般情况,使用 self performSelector:SEL withObject:id方法

  1. [self performSelectorOnMainThread:@selector(testAA:) withObject:[NSArray arrayWithObjects:@"1",@"2", nil nil] waitUntilDone:NO];  
  2.   
  3.   
  4. -(void) testAA:(NSArray*)data{  
  5.    
  6.     if (data==nil||data.count!=2) {  
  7.         return;  
  8.     }  
  9.     NSInteger num=[(NSString*)data[0] intValue];  
  10.     NSInteger index=[data[1] intValue];  
  11. }  


二、NSTimer:将传参的对象储存在了NSTimer的userInfo的字典中。

 
  1. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];    
  2. [dict setObject:oldView forKey:@"oldView"];  
  3. [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(onTimer:) userInfo:dict repeats:NO];    
  4. [dict release];  
  5.   
  6. - (void)onTimer:(NSTimer *)timer {    
  7.     UIView *oldView = [[timer userInfo] objectForKey:@"oldView"];  
  8. }  


三、UIButton类:通过设置tag来使用

 
  1. UIButton * markButton=[[UIButton alloc] initWithFrame:CGRectMake(280, 0, 30, 30)];  
  2. markButton.tag=@"参数值"; //这里是你要传递的参数值  
  3. [markButton addTarget:self action:@selector(addMark:)  forControlEvents:UIControlEventTouchUpInside];  
  4. -(BOOL) addMark:(UIButton *)btn {  
  5.     NSLog(@"%@",btn.tag];  

posted on 2016-02-16 20:26  Mr_Deng  阅读(2077)  评论(0编辑  收藏  举报

导航