【发布时间】:2014-01-18 22:53:09
【问题描述】:
我正在使用下面定义的类来保存选择器和选择器。在我想要执行选择器的选择器的 didSelectRow 方法上。但是,当我使用选择器更改行时,我不断收到“无法识别的选择器发送到实例”异常。
我尝试将选择器的声明更改为“SEL *theSelector”,但这并没有带来任何乐趣,因为当调用 performSelector 时 theSelector 为 NULL。
任何修复/想法将不胜感激。提前致谢。
带选择器的类:
@implementation ClassA{
UIPickerView *thePicker;
SEL theSelector;
}
-(id)initWithView:(UIView*)theView{
thePicker = [[UIPickerView alloc] init];
thePicker.showsSelectionIndicator = YES;
thePicker.delegate = self;
thePicker.dataSource = self;
[theView addSubview:thePicker];
theSelector = NULL;
}
-(void)setSelector:(SEL)selector{
theSelector = selector;
}
-(void)performTheSelector{
if (theSelector != NULL) {
[self performSelector:theSelector onThread:[NSThread currentThread] withObject:self waitUntilDone:YES];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component {
[self performTheSelector];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 10;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return @"Some Row";
}
创建 ClassA 实例并设置选择器的类:
@implementation ClassB{
}
-(void)initWithView:(UIView*)theView{
ClassA *objectA = [ClassA alloc] initWithView:theView]];
[objectA setSelector:@selector(theSelectorMethod:)];
}
-(IBAction)theSelectorMethod:(id)sender{
//do something
}
【问题讨论】:
-
不应声明为
SEL *。错误消息中无法识别的选择器是什么?在 ivar 中的SEL还是其他?你为什么做这个? (另请注意,您不需要将 ivars 初始化为 0 - 这是由alloc完成的。此外,您的init需要return self。) -
上线引发异常:[self performSelector:theSelector onThread:[NSThread currentThread] withObject:self waitUntilDone:YES];
-
但是错误信息的全文是什么?什么是无法识别的选择器本身?
-
'NSInvalidArgumentException',原因:'-[ClassA theSelectorMethod:]:无法识别的选择器发送到实例 0x8d391b0'
标签: objective-c selector uipickerview