1

I'm using a ViewController @interface PagerViewController : UIViewController <UIScrollViewDelegate> where I add other ViewControllers as a child ( addChildViewController ) Those ViewControllers that I add are from difference classes but they all inherit from UIViewController. I can loop thru them using this code:

    for(NSUInteger i = 0; i<(unsigned int)[_app.windows count]; i++) {

    NSLog(@"%@",((MyViewController*)[self.childViewControllers objectAtIndex:i]).getQAnswer);

}

However, I always need to cast it to the correct one (in the above example I cast it to MyViewController. The problem is that there are many childs from different classes on it, so what if I only want the childs from the class "anotherViewController" instead of "MyViewController"?

4

2 回答 2

0

You can use isKindOfClass to filter those objects which are instances of a certain class. for example:

if ([object isKindOfClass:[anotherViewController class]]) {

}
于 2013-03-21T09:47:22.697 回答
0

If you want objects from certain class, use isMemberOfClass:, if you want to include descendant classes, use isKindOfClass:.
E.g. if arr is NSMutableArray then [arr isKindOfClass:[NSArray class]] is TRUE and[arr isMemberOfClass:[NSArray class]] is FALSE.

于 2013-03-21T09:54:49.350 回答