我在使用块和继承时遇到了一些极端困难。
我在 .h 文件中声明了一个块:
void (^block)(BOOL);
在.m中初始化它:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
block = ^(BOOL noshow)
{
if(noshow){
AutoTracView.hidden = YES;
MarineTracView.hidden = YES;
}
};
}
return self;
}
现在我将它以及 UIVIEw 实例传递给 UIViewController 的类别:
NSDictionary *const collection = [NSDictionary dictionaryWithObjectsAndKeys: //make global
@"1", AutoTracView,
@"2", MarineTracView,
nil];
[self renderView:[current_unit.unit_type intValue] onCollection:(NSDictionary *)collection withBlock:block];
在 UIViewController+extensions.m 中:
-(void)renderView:(int)value onCollection:(NSDictionary *)collection withBlock:block
{
block(TRUE);
NSString *index = [NSString stringWithFormat:@"%d", value];
UIView *view = [collection objectForKey:index];
view.hidden = NO;
}
不幸的是,block(TRUE) 抛出编译错误:“调用的对象类型 id 不是函数或函数指针”
好的,所以我很绝望,只是尝试让块的工作正常,所以我将块作为参数调用删除。并在确实有效的控制器中调用它。但随之而来的另一个问题是:
NSDictionary *const collection = [NSDictionary dictionaryWithObjectsAndKeys: //make global
@"1", AutoTracView,
@"2", MarineTracView,
nil];
上面的说法失败了:
-[UIView copyWithZone:]: unrecognized selector sent to instance 0x6ee2b80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance 0x6ee2b80'
我不允许传递我的 UIView 实例吗?
这是头文件:
@property (weak, nonatomic) IBOutlet UIView *AutoTracView;
@property (weak, nonatomic) IBOutlet UIView *MarineTracView;
我现在觉得做任何事情都被困住了。这似乎是一个两部分的问题,但实际上我放弃了传递块的概念,只是寻找关于错误“[UIView copyWithZone:]: unrecognized selector sent to instance”的想法