3

我在使用块和继承时遇到了一些极端困难。

我在 .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”的想法

4

3 回答 3

3

一个问题是您对dictionaryWithObjectsAndKeys:. 我认为您想让 UIViews 成为对象,将数字字符串作为键,但是按照您的编码方式,您将字符串作为值,将 UIViews 作为键。UIView 没有实现 NSCopyable,因此它们不能用作键。您应该在通话中颠倒顺序,如下所示:

NSDictionary *const collection = [NSDictionary dictionaryWithObjectsAndKeys: //make global
                                  AutoTracView, @"1",
                                  MarineTracView, @"2",
                                  nil];

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.

于 2012-05-24T21:54:20.967 回答
0

方法定义呢?是这样的:

-(void)renderView:(int)value onCollection:(NSDictionary *)collection withBlock:(void(^)(BOOL))block;

也许您没有以正确的方式定义类别中的方法

希望这可以帮助

于 2012-05-24T21:33:04.347 回答
0

A dictionary's keys are copied, and your code attempts to use instances of UIView as keys. I suspect you have your argument order wrong and meant to use the strings as keys. When using a variadic argument list to initialize a dictionary, the values come first.

于 2012-05-24T21:56:05.320 回答