iOS之@selector的函数传递多个参数

xiaoxiao2021-02-28  148

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

[objc]  view plain  copy [self performSelectorOnMainThread:@selector(testAA:) withObject:[NSArray arrayWithObjects:@"1",@"2", nil nil] waitUntilDone:NO];         -(void) testAA:(NSArray*)data{           if (data==nil||data.count!=2) {           return;       }       NSInteger num=[(NSString*)data[0] intValue];       NSInteger index=[data[1] intValue];   }   2、NSTimer:将传参的对象储存在了NSTimer的userInfo的字典中。

[objc]  view plain  copy NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];     [dict setObject:oldView forKey:@"oldView"];   [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"];   }   3、UIButton类:通过设置tag来使用

[objc]  view plain  copy UIButton * markButton=[[UIButton alloc] initWithFrame:CGRectMake(28003030)];   markButton.tag=@"参数值"//这里是你要传递的参数值   [markButton addTarget:self action:@selector(addMark:)  forControlEvents:UIControlEventTouchUpInside];      -(BOOL) addMark:(UIButton *)btn {       NSLog(@"%@",btn.tag];   }   4、如果UIButton中,要传的参数是个对象,写一个UIButton的子类,然后同3

[objc]  view plain  copy @interface CellButton:UIButton @property(nonatomic,retain) NSString *fileName; @end [objc]  view plain  copy cellForRowAtIndexPath{ ……   cell.btnOpen.fileName = @"XXX"; [cell.btnOpen addTarget:self action:@selector(clickBtnOpen:) forControlEvents:UIControlEventTouchDown];

  …… }

虽然有参数,但是selector()中还是只写了clickBtnOpen,但有参数要加冒号

5、设置关联(还没看)

objc_setAssociatedObject

objc_getAssociatedObject http://blog.csdn.net/chenglibin1988/article/details/27549925

http://m.blog.csdn.net/japhoo/article/details/51201161

转载请注明原文地址: https://www.6miu.com/read-21238.html

最新回复(0)