IPhone - how to use performSelector with complex parameters?

I have an application designed for iPhone OS 2.x.

At some point I have this code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //... previous stuff initializing the cell and the identifier cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:myIdentifier] autorelease]; // A // ... more stuff } 

But since the initWithFrame selector is deprecated in 3.0, I need to convert this code with responseToSelector and execute a Selector ... like this ...

 if ( [cell respondsToSelector:@selector(initWithFrame:)] ) { // iphone 2.0 // [cell performSelector:@selector(initWithFrame:) ... ???? what? } 

My problem: how can I break the call of A into preformSelector if I need to pass two parameters: initWithFrame: CGRectZero and "reuseIdentifier: myIdentifier" ???

EDIT - As fbrereto said, I did it

  [cell performSelector:@selector(initWithFrame:reuseIdentifier:) withObject:CGRectZero withObject:myIdentifier]; 

I have an "incompatible type error for argument 2 of 'performSelector: withObject: withObject' .

myIdentifier is declared as follows

 static NSString *myIdentifier = @"Normal"; 

I tried to change the call

  [cell performSelector:@selector(initWithFrame:reuseIdentifier:) withObject:CGRectZero withObject:[NSString stringWithString:myIdentifier]]; 

without success ...

Another point: CGRectZero is not an object ...

+4
source share
2 answers

Use NSInvocation .

  NSInvocation* invoc = [NSInvocation invocationWithMethodSignature: [cell methodSignatureForSelector: @selector(initWithFrame:reuseIdentifier:)]]; [invoc setTarget:cell]; [invoc setSelector:@selector(initWithFrame:reuseIdentifier:)]; CGRect arg2 = CGRectZero; [invoc setArgument:&arg2 atIndex:2]; [invoc setArgument:&myIdentifier atIndex:3]; [invoc invoke]; 

Alternatively, call objc_msgSend directly (skipping all unnecessary complex high-level constructs):

 cell = objc_msgSend(cell, @selector(initWithFrame:reuseIdentifier:), CGRectZero, myIdentifier); 
+11
source

The selector you want to use is actually @selector(initWithFrame:reuseIdentifier:) . To pass two parameters, use performSelector:withObject:withObject: For the right choice of parameters, a small trial version and an error may be required, but it should work. If this is not the case, I would recommend exploring the NSInvocation class, which is designed to handle more complex message dispatch.

+1
source

Source: https://habr.com/ru/post/1303348/


All Articles