I am having some extreme difficulty with this using blocks and inheritance.
I declared a block in .h file:
void (^block)(BOOL);
initialize it in .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;
}
Now I pass it as well as UIVIEw instances to a category of 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];
In 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;
}
Unfortunately, block(TRUE) throws a compile error: “called object type id is not a function or function pointer”
ok, so I get desparate, and just try to get the block thing working so i remove the block as a parameter call. and just call it within the controller which does work. But then another problem arises:
NSDictionary *const collection = [NSDictionary dictionaryWithObjectsAndKeys: //make global
@"1", AutoTracView,
@"2", MarineTracView,
nil];
It fails with that above saying:
-[UIView copyWithZone:]: unrecognized selector sent to instance 0x6ee2b80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView copyWithZone:]: unrecognized selector sent to instance 0x6ee2b80'
Am I not allowed to pass my UIView instances around?
Here is the header file for that:
@property (weak, nonatomic) IBOutlet UIView *AutoTracView;
@property (weak, nonatomic) IBOutlet UIView *MarineTracView;
Im feeling trapped right now in doing anything. This might seem like a two part question but really i abandoned the concept of passing blocks and just looking for ideas on the error “[UIView copyWithZone:]: unrecognized selector sent to instance “
One issue is your call to
dictionaryWithObjectsAndKeys:. I think you want to make the UIViews the objects, and the number strings the keys, but the way you’ve coded it, you’re making the strings the values and the UIViews the keys. UIView doesn’t implement NSCopyable, so they can’t be used as keys. You should reverse the order in your call, like so:Really, if all you want to do is give them numeric indexes, you could use an NSArray instead of an NSDictionary, but there’s no reason it won’t work either way.