How to identify input in methods eg. Subclasses of UITableViewController
I have the following issue:
I want to create a method which accepts input from different subclasses of UITableViewController. If I identify the input as (id) the compiler won't accept any methods on the custom table view controllers:
-(void)addViewControllerSubview:(id)viewController {
[viewController customMethod]; //Compiler does not allow this method
}
What 开发者_StackOverflowis the way to identify the input so that I can use different subclasses of UITableViewController and at the same time my custom methods?
Thanks for any help!
While you can send a message to any object (id) - property accessors require that the compiler be aware of the type you are dealing with - this is because property accessors are syntactic sugar around calling specific getter and setter methods.
Instead of using (id)
, use the class name (CustomViewController *)
to alert the compiler to the class that you're using.
Another option is to do something like this:
-(void)addViewControllerSubview:(id)viewController {
CustomViewController *vc = (CustomViewController *)viewController;
[vc customMethod];
}
Either way, be sure to import the header for the custom class.
EDIT: If you have multiple custom view controllers, the you can use if ([viewController isKindOfClass:[CustomViewController class]])
精彩评论