IOS 7无法识别的选择器发送到带有按钮的实例问题
Posted
技术标签:
【中文标题】IOS 7无法识别的选择器发送到带有按钮的实例问题【英文标题】:IOS 7 unrecognized selector sent to instance issue with button 【发布时间】:2014-09-17 09:07:02 【问题描述】:我有一个 JSON 响应对象,存储如下所示:
ownerEvents = [NSMutableArray arrayWithArray:[(NSDictionary *)responseObject objectForKey:@"result"]];
ownerEvents 存储下面的 JSON(通过 xcode 调试窗口检索):
2014-09-17 01:23:49.311 test123[31919:60b] JSON:
result = (
"end_time" = "4:00AM";
"event_date" = "09-16-2014";
"event_id" = 89;
"event_image" = "http://www.jessetaveras.com/test123/images/event_flyers/event1141091689687507968f3f78ac5f23b2a954bb1dc6d.png";
"event_price" = 30;
"event_tickets_total" = 250;
"event_title" = dfjjsnda;
"start_time" = "5:00AM";
"venue_name" = "the shit box";
weekday = Tuesday;
"end_time" = "4:00AM";
"event_date" = "09-16-2014";
"event_id" = 89;
"event_image" = "http://www.jessetaveras.com/test123/images/event_flyers/event1141091689687507968f3f78ac5f23b2a954bb1dc6d.png";
"event_price" = 30;
"event_tickets_total" = 250;
"event_title" = dfjjsnda;
"start_time" = "5:00AM";
"venue_name" = "the shit box";
weekday = Tuesday;
);
然后将上面的 JSON 拆分为表格视图中的 2 个不同的单元格
我需要将单个单元格的数据发送到另一个视图控制器。在应该将数据发送到另一个视图控制器的函数内部,我有以下代码:
在我拥有的单元格类中:
@property (strong, nonatomic) IBOutlet UIButton *editButtonPressed;
在 CellForRowAtIndexPath(在 UIViewController 中)我有:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"OwnerCell";
TDOwnerViewCell *historyCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
historyCell.selectionStyle = UITableViewCellSelectionStyleNone;
//NSLog(@"THIS IS current Cell %@", [ownerEvents objectAtIndex:indexPath.row]);
[historyCell configOwnerEventCell:[ownerEvents objectAtIndex:indexPath.row]];
[historyCell.editButtonPressed addTarget:self action:@selector(editButtonPressed) forControlEvents:UIControlEventTouchUpInside];
historyCell.editButtonPressed.tag = indexPath.row;
if(historyCell == nil)
historyCell = [[TDOwnerViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OwnerCell"];
return historyCell;
这是按下按钮时调用的方法:
- (void)editButtonPressed:(UIButton *) button
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
TDEventEditViewController * vc3 = [storyboard instantiateViewControllerWithIdentifier:@"eventEditView"];
[historyCell configOwnerEventCell:[ownerEvents objectAtIndex:indexPath.row]];
NSInteger index = button.tag;
NSArray *eventsJSONData = [ownerEvents objectAtIndex:index];
ownerEvents = [NSMutableArray arrayWithArray:[(NSDictionary *)responseObject objectForKey:@"result"]];
vc3.detailDic = eventsJSONData[index];
[self.navigationController pushViewController:vc3 animated:YES];
我收到此错误:
-[TDOwnerViewCell editButtonPressed:]: unrecognized selector sent to instance 0xca00000
2014-09-17 01:46:21.884 test123[31919:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TDOwnerViewCell editButtonPressed:]: unrecognized selector sent to instance 0xca00000'
*** First throw call stack:
(
0 CoreFoundation 0x034391e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x031b88e5 objc_exception_throw + 44
2 CoreFoundation 0x034d6243 - [NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0342950b ___forwarding___ + 1019
4 CoreFoundation 0x034290ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x031ca880 - [NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x01e7a3b9 - [UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x01e7a345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x01f7bbd1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x01f7bfc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x01f7b243 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x022102e3 _UIGestureRecognizerUpdate + 7166
12 UIKit 0x01eb9a5a -[UIWindow _sendGesturesForEvent:] + 1291
13 UIKit 0x01eba971 -[UIWindow sendEvent:] + 1021
14 UIKit 0x01e8c5f2 -[UIApplication sendEvent:] + 242
15 UIKit 0x01e76353 _UIApplicationHandleEventQueue + 11455
16 CoreFoundation 0x033c277f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
17 CoreFoundation 0x033c210b __CFRunLoopDoSources0 + 235
18 CoreFoundation 0x033df1ae __CFRunLoopRun + 910
19 CoreFoundation 0x033de9d3 CFRunLoopRunSpecific + 467
20 CoreFoundation 0x033de7eb CFRunLoopRunInMode + 123
21 GraphicsServices 0x04c475ee GSEventRunModal + 192
22 GraphicsServices 0x04c4742b GSEventRun + 104
23 UIKit 0x01e78f9b UIApplicationMain + 1225
24 test123 0x000e3f9d main + 141
25 libdyld.dylib 0x039b6701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
请帮忙,我似乎从来没有弄清楚这些错误。提前致谢。
【问题讨论】:
为您的项目添加异常断点。这样,当它崩溃时,它会将您带到导致崩溃的代码行。 您在单元格上调用 editButtonPressed 并且该方法不存在。 TDEventEditViewController中有UITabelView吗? 试试这个:替换 vc3.detailDic = eventsJSONData[index]; with vc3.detailDic = eventsJSONData; 如果您在那里创建了相应的自定义 tableviewcell,您是否将方法 editButtonPressed 映射到了。 【参考方案1】:editButtonPressed 方法可能与故事板中的不同方法连接只需转到故事板并检查按钮与方法的连接
【讨论】:
按钮设置为单元格类中的属性。按下按钮时使用的函数在表视图控制器的类中。这是 cellForRowAtIndexPath 中的代码: TDOwnerViewCell *historyCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ]; [historyCell configOwnerEventCell:[ownerEvents objectAtIndex:indexPath.row]]; [historyCell.editButtonPressed addTarget:self action:@selector(editButtonPressed) forControlEvents:UIControlEventTouchUpInside]; historyCell.editButtonPressed.tag = indexPath.row; 所以有一个方法叫做editbuttonPressed所以可能有问题 您是否将单元格的编辑按钮从情节提要连接到 TDOwnerViewCell.h 我猜你必须像@selector(editButtonPressed:)一样在editButtonPressed的末尾加一个冒号 让我们continue this discussion in chat。【参考方案2】:您调用的此 editButtonPressed 方法未连接或未出现在您的逻辑中。
-[TDOwnerViewCell editButtonPressed:]: unrecognized selector sent to instance 0xca00000
2014-09-17 01:46:21.884 test123[31919:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TDOwnerViewCell editButtonPressed:]: unrecognized selector sent to instance 0xca00000'
那么你能分享一下你在 VC 之间传递数据的方法吗?
更新:- 首先,在 cellForRowAtIndexPath 方法中改变流程如下。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"OwnerCell";
TDOwnerViewCell *historyCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
//You need to check if historyCell is nil or not firstly then move on to your further populating logic.
if(historyCell == nil)
historyCell = [[TDOwnerViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OwnerCell"];
historyCell.selectionStyle = UITableViewCellSelectionStyleNone;
[historyCell configOwnerEventCell:[ownerEvents objectAtIndex:indexPath.row]];
[historyCell.editButtonPressed addTarget:self action:@selector(editButtonPressed) forControlEvents:UIControlEventTouchUpInside];
historyCell.editButtonPressed.tag = indexPath.row;
return historyCell;
在您的情况下,问题可能是当您检查 historyCell 是否结束时,如果它是 nil,则返回一个新的历史单元格。
所以试试这个,如果有任何问题,请告诉我。
【讨论】:
【参考方案3】:我会设置一个断点并逐步执行每一位,确保一切都按照您的想法进行初始化。
例如,这真的会被填充吗?
NSArray *eventsJSONData = [ownerEvents objectAtIndex:index];
再一次,这真的会被填充吗?
vc3.detailDic = eventsJSONData[index];
【讨论】:
该错误与TDOwnerViewCell
的实例上缺少方法有关 - 它与 JSON 数据或可变数组无关
@Paulw11 我也怀疑 SO 对 JSON 数据的解析不正确
堆栈跟踪清楚地表明-[TDOwnerViewCell editButtonPressed:]: unrecognized selector sent to instance 0xca00000
。错误不在显示的代码中。它可能是由情节提要或笔尖中按钮上的事件处理程序引起的
@Paulw11 仅对于此处给出的堆栈跟踪和代码,我们无法弄清楚是什么代码导致了问题。可能是解析前的代码。你是对的,除了我们在这里讨论之外,堆栈跟踪给出了不同的错误。但这只是 SO 担心他的解析可能是错误的
我发现我可能错误地认为问题出在 JSON 上,您对我应该将问题的标题更改为什么有任何建议吗?以上是关于IOS 7无法识别的选择器发送到带有按钮的实例问题的主要内容,如果未能解决你的问题,请参考以下文章
ios [__NSCFNumber 长度]:发送到实例的无法识别的选择器
iOS---导致无法识别的选择器发送到实例 0x7f803a59cc50 的按钮
Google Play 游戏服务 iOS:[UITableViewWrapperView 样式]:发送到实例的无法识别的选择器