Dan
Dan

Reputation: 5637

Can I use the same method passing in different UIViewController subclasses?

I've got a custom class that goes away and talks to a web service. At the moment I have a UIViewController sub class using my service class and getting data back fine.

I send a reference of my UIViewController sub class to the service query method (queryServiceWithParent) as a method parameter.

I then store the UIViewController sub class in a property so I can use it later on in the class.

NeighbourhoodData.h:

NeighbourhoodDetailTableViewController *viewController;

NeighbourhoodData.m:

- (void)queryServiceWithParent:(UIViewController *)controller {

    viewController = (NeighbourhoodDetailTableViewController *)controller;

}

My problem is that I now would like to use this class from another view with a different UIViewController sub class.

I can pass the sub class through the same method because my parameter is only a UIViewController but it's matter of setting my class property to the correct UIViewController subclass type.

Is there a way I can do this? I'm very new to the whole iPhone development scene so the only way I would think of doing this is to create a new class, but I would have thought there has to be a better way...

Upvotes: 1

Views: 354

Answers (2)

Simon Goldeen
Simon Goldeen

Reputation: 9080

you can ask an id what kind of class it is. Something like this might work:

- (void)doStuffWithMyViewController:(UIViewController *)controller
{
    if ([controller isKindOfClass:[FirstViewControllerSubclass class]])
    {
        //The controller is a FirstViewControllerSubclass or a subclass of FirstViewControllerSubclass
    }

    if ([controller isKindOfClass:[SecondViewControllerSubclass class]])
    {
        //The controller is a SecondViewControllerSubclass or a subclass of SecondViewControllerSubclass
    }
}

From there you can do different things based on which subclass they are or whatever you want.

Another thing to consider is making a common superclass for the two view controllers if there is shared functionality between them

Upvotes: 1

Seyther
Seyther

Reputation: 662

Either you create a class method for other classes to access or you would have to learn to set up delegations to make your current class a delegate to other classes.

Upvotes: 0

Related Questions