Ссылка на видеоурок.
Селекторы это названия методов.
Пример синтаксиса для разных методов с параметрами и без:
SEL selector = @selector(testMethod);
SEL selector1=@selector(testMethod:);
SEL selector2=@selector(testMethod:andParametr2:);
SEL secretSelector=@selector(superSecretText);
SEL selInt=@selector(testMethodWithInteger:);
NSInteger i=11;
Вызов методов через селекторы с разным количеством параметров.
[self performSelector:selector];
[self performSelector:selector1 withObject:@"string"];
[self performSelector:selector2 withObject:@"string" withObject:@"string2"];
Вызов метода с задержкой
[self performSelector:selector withObject:nil afterDelay:3];
TAObject *testObject=[[TAObject alloc]init];
[testObject performSelector:selector];
С помощью селектора можно вызвать метод, который не указан в h файле, но есть в реализации.
NSLog(@"Secret %@",[testObject performSelector:secretSelector] );
NSLog(@"Secret %@",[self performSelector:selInt withObject:[NSNumber numberWithInt:i]] );
Преобразование в строку из селектора и наоборот:
NSString* str= NSStringFromSelector(selector1);
SEL sel=NSSelectorFromString(str);
через селекторы легко вызывать методы с параметрами - объектами. для вызова с параметрами -простыми типами (int,float,double) не сильно просто.
В качестве примера приводится текст вызова и сам метод
SEL selec=@selector(testMethodParametr1: parametr2: parametr3:);
NSMethodSignature *signature=[AppDelegate instanceMethodSignatureForSelector:selec ];
NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:selec];
NSInteger iVal=2;
CGFloat fVal=3.1;
double dVal=5.3f;
NSInteger *p=&iVal;
CGFloat *p2=&fVal;
double *p3=&dVal;
[invocation setArgument:p atIndex:2];
[invocation setArgument:p2 atIndex:3];
[invocation setArgument:p3 atIndex:4];
[invocation invoke];
NSString * string =nil;
[invocation getReturnValue:&string];
NSLog(@"string= %@",string );
return YES;
}
-(NSString*) testMethodParametr1: (NSInteger) intValue parametr2: (CGFloat) floatValue parametr3: (double) doubleValue{
return [NSString stringWithFormat:@"int: %@,float: %@, double: %@",@(intValue),@(floatValue),@(doubleValue)];
}
Проект с исходником урока
Селекторы это названия методов.
Пример синтаксиса для разных методов с параметрами и без:
SEL selector = @selector(testMethod);
SEL selector1=@selector(testMethod:);
SEL selector2=@selector(testMethod:andParametr2:);
SEL secretSelector=@selector(superSecretText);
SEL selInt=@selector(testMethodWithInteger:);
NSInteger i=11;
Вызов методов через селекторы с разным количеством параметров.
[self performSelector:selector];
[self performSelector:selector1 withObject:@"string"];
[self performSelector:selector2 withObject:@"string" withObject:@"string2"];
Вызов метода с задержкой
[self performSelector:selector withObject:nil afterDelay:3];
TAObject *testObject=[[TAObject alloc]init];
[testObject performSelector:selector];
С помощью селектора можно вызвать метод, который не указан в h файле, но есть в реализации.
NSLog(@"Secret %@",[testObject performSelector:secretSelector] );
NSLog(@"Secret %@",[self performSelector:selInt withObject:[NSNumber numberWithInt:i]] );
Преобразование в строку из селектора и наоборот:
NSString* str= NSStringFromSelector(selector1);
SEL sel=NSSelectorFromString(str);
через селекторы легко вызывать методы с параметрами - объектами. для вызова с параметрами -простыми типами (int,float,double) не сильно просто.
В качестве примера приводится текст вызова и сам метод
SEL selec=@selector(testMethodParametr1: parametr2: parametr3:);
NSMethodSignature *signature=[AppDelegate instanceMethodSignatureForSelector:selec ];
NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:selec];
NSInteger iVal=2;
CGFloat fVal=3.1;
double dVal=5.3f;
NSInteger *p=&iVal;
CGFloat *p2=&fVal;
double *p3=&dVal;
[invocation setArgument:p atIndex:2];
[invocation setArgument:p2 atIndex:3];
[invocation setArgument:p3 atIndex:4];
[invocation invoke];
NSString * string =nil;
[invocation getReturnValue:&string];
NSLog(@"string= %@",string );
return YES;
}
-(NSString*) testMethodParametr1: (NSInteger) intValue parametr2: (CGFloat) floatValue parametr3: (double) doubleValue{
return [NSString stringWithFormat:@"int: %@,float: %@, double: %@",@(intValue),@(floatValue),@(doubleValue)];
}
Проект с исходником урока
Комментариев нет:
Отправить комментарий