iPhone – 如何使用带复杂参数的performSelector?

iPhone – 如何使用带复杂参数的performSelector?,第1张

概述我有一个专为iPhone OS 2.x设计的应用程序. 在某些时候我有这个代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //... previous stuff initializing the cell and the i 我有一个专为iPhone OS 2.x设计的应用程序.

在某些时候我有这个代码

- (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}

但由于initWithFrame选择器在3.0中已弃用,我需要使用respondToSelector和performSelector转换此代码……因此…

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

我的问题是:如果我必须传递两个参数“initWithFrame:CGRectZero”和“reuseIDentifIEr:myIDentifIEr”,我如何将A上的调用断开到preformSelector调用?

编辑 – 由于fbrereto的消化,我做到了这一点

[cell performSelector:@selector(initWithFrame:reuseIDentifIEr:)    withObject:CGRectZero     withObject:myIDentifIEr];

我遇到的错误是“performSelector:withObject:withObject”的参数2的不兼容类型.

myIDentifIEr是这样声明的

static Nsstring *myIDentifIEr = @"normal";

我试图将呼叫改为

[cell performSelector:@selector(initWithFrame:reuseIDentifIEr:)    withObject:CGRectZero     withObject:[Nsstring stringWithString:myIDentifIEr]];

没有成功…

另一点是CGRectZero不是一个对象……

解决方法 使用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];

或者,直接调用objc_msgSend(跳过所有不必要的复杂高级构造):

cell = objc_msgSend(cell,@selector(initWithFrame:reuseIDentifIEr:),CGRectZero,myIDentifIEr);
总结

以上是内存溢出为你收集整理的iPhone – 如何使用带复杂参数的performSelector?全部内容,希望文章能够帮你解决iPhone – 如何使用带复杂参数的performSelector?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存