目标C:无法识别的选择器发送到UIViewController子类的子类

时间:2011-02-08 15:09:08

标签: objective-c inheritance uiviewcontroller selector

差不多一周,我无法解决脑部疼痛问题:

我有一个名为StreamingViewControllerCommon的UIViewController子类实现了这个属性:

@property (nonatomic, assign, readonly) BOOL isMusicStopped;

当然我在.m文件中对它进行了@synthesize

然后我有这个类的2个子类:Listen_UIViewControllerLastNews_UIViewController修改(不调用self.isMusicStopped但直接访问它)var isMusicStopped

在另一个类中,我有一个NSMutableDictionary包含这两个类的2个实例(每个类1个)但是当我尝试这样做时:

if (streamingViews){
        for(StreamingViewControllerCommon* aView in streamingViews){
            BOOL stopped = aView.isMusicStopped;
            NSLog(@"%@",stopped);
            if(stopped){
                [aView closeStream];
                [streamingViews removeObjectForKey:[aView class]];
                [aView release];
                aView = nil;
            }
        }
    }

我收到此错误:

2011-02-08 15:55:09.760 ProjectName[6182:307] +[LastNews_UIViewController isMusicStopped]: unrecognized selector sent to class 0x2143c
2011-02-08 15:55:09.768 ProjectName[6182:307] CoreAnimation: ignoring exception: +[LastNews_UIViewController isMusicStopped]: unrecognized selector sent to class 0x2143c

但奇怪的是,在StreamingViewControllerCommon中还实现了以下方法:

-(void) destroyStreamer{
}

-(void) closeStream{
    [self destroyStreamer];
}

当我这样做时:

NSMutableArray* keys = [[NSMutableArray alloc] initWithArray:[streamingViews allKeys]];
    [keys removeObject:[thisView class]];
    NSMutableArray *tmp =  [[NSMutableArray alloc] initWithArray:[streamingViews objectsForKeys:keys notFoundMarker:@"404"]];
    if (tmp){
        for (StreamingViewControllerCommon* vc in tmp)
            [vc closeStream];
        [tmp release];
    }

    [streamingViews removeObjectsForKeys:keys];

我没有收到任何错误,并且正确调用了子类的重写的closeStream方法。

我做错了什么?

最诚挚的问候,安东尼奥

编辑:正如我在评论中写的:改变这个:

if (streamingViews){
        for(StreamingViewControllerCommon* aView in streamingViews){
            BOOL stopped = aView.isMusicStopped;
            NSLog(@"%@",stopped);
            if(stopped){
                [aView closeStream];
                [streamingViews removeObjectForKey:[aView class]];
                [aView release];
                aView = nil;
            }
        }
    }

在此:

if (streamingViews){
        for (id aViewClass in streamingViews){
            StreamingViewControllerCommon* aView = [[streamingViews objectForKey:aViewClass] retain]; 
            //NSLog(@"%@",aView.isMusicStopped);
            if(aView.isMusicStopped){
            [aView closeStream];
            [streamingViews removeObjectForKey:aViewClass];
            [aView release];
            aView = nil;
        }
    }
}

诀窍:P NSDictionary的每个循环返回其键而不是对象,因为我使用对象类作为键我得到了奇怪的异常

1 个答案:

答案 0 :(得分:2)

无论出于何种原因,看起来streamingViews不包含LastNews_UIViewController的实例,而是包含类本身。这就是+[LastNews_UIViewController isMusicStopped]中加号的含义。