Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 不良访问&;使用UIPickerView时出现NSInvalidArgumentException_Ios_Objective C_Delegates_Datasource_Uipickerview - Fatal编程技术网

Ios 不良访问&;使用UIPickerView时出现NSInvalidArgumentException

Ios 不良访问&;使用UIPickerView时出现NSInvalidArgumentException,ios,objective-c,delegates,datasource,uipickerview,Ios,Objective C,Delegates,Datasource,Uipickerview,我是UIKit的初学者。当我厌倦了在iOS 7 SDK中使用UIPickerView时,我遇到了一个难以解决的问题 我们知道UIPickerView需要两种资源才能完美工作:数据源和委托。所以我写了一个名为“KMPickerProtocols”的类。我将其应用于UIPickerViewDataSource和UIPickerViewDelegate协议,然后向其添加了一些额外的setter方法 KMPickerProtocols适用于数据源和委托协议的所有基本方法+设置每行标题的必要可选方法(pi

我是UIKit的初学者。当我厌倦了在iOS 7 SDK中使用UIPickerView时,我遇到了一个难以解决的问题

我们知道UIPickerView需要两种资源才能完美工作:数据源和委托。所以我写了一个名为“KMPickerProtocols”的类。我将其应用于UIPickerViewDataSource和UIPickerViewDelegate协议,然后向其添加了一些额外的setter方法

KMPickerProtocols适用于数据源和委托协议的所有基本方法+设置每行标题的必要可选方法(pickerView:titleForRow:forComponent)。所有这些方法(在正常情况下)都能顺利完成任务

最后,我使用以下代码手动设置UIPickerView(名为_accountPicker)的委托和数据源属性:

 NSArray *delegateAgent = [[KMTwitterDelegate new] run ];
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
_accountPicker.delegate = [delegateAgent objectAtIndex:1] ;
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([KMAppDelegate class]));
}
(run方法用于设置一些属性,包括每行的高度和…)

现在,当我运行我的应用程序时,它将以UIPickerView的形式显示存储在系统(帐户框架)中的我的twitterAccounts。但有一个问题:只要我滚动选取器视图或点击任何一行,程序就会崩溃,我在这行代码中获得了错误的访问(代码=2,地址=0x1):

 NSArray *delegateAgent = [[KMTwitterDelegate new] run ];
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
_accountPicker.delegate = [delegateAgent objectAtIndex:1] ;
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([KMAppDelegate class]));
}
有时我也会在上面的线路上得到信号SIGABRT。在这些情况下,日志显示:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSMallocBlockpickerView:titleForRow:forComponent::]:无法识别的选择器已发送到实例0x8c75ad0'

如果你能帮我解决这个问题,我将不胜感激。我真的不知道臭虫在哪里。但我想这个屏幕截图将有助于发现:


上面截图中的问题是UIPicker为第0行调用了“pickerView:titleForRow:forComponent”方法三次,而不是一次。我只是不知道这是因为我的错还是因为UIPicker的典型行为

您正在向类型为
NSMallocBlock
的对象发送消息
pickerView:titleForRow:forComponent:
。这是一条从选择器自动发送到其数据源的消息,因此我假设您正在分配数据源:

_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
您分配的数据源不符合协议
UIPickerViewDataSource


签出返回的内容
[delegateAgent对象索引:0]并确保它是您所期望的。

谢谢。我检查过了,这正是我所期望的。在应用程序的第一次运行中,没有问题,但是当你玩picker时,这些不好的事情就会发生。