Neil
Neil

Reputation: 2058

Best way to call method that exists in multiple classes

I have a main UIViewController where most of the users' interaction happens. In the main ViewController there are three subviews. The user can load separate ViewControllers into the UIView subviews.

Each of the subviews that are loaded deal with data entry. In turn, firstResponders are called. I would like to be able to dismiss the firstRespnders through the main ViewController, maybe with a 'Done' button.

I was thinking I could add a method in each of the separate subviews with one name ex;

-(void) methodToResignResponders {}

Then, in the main ViewController call this method to the view that is currently open to the user. In turn resigning the responders that are active in the subview.

Further Information:

This is how I set up each view as a subview of the main ViewController:

UIViewController *calcVC;



//set up the view to be added depending on the name of the view that was passed

if ([viewName isEqualToString:@"Tax"]) {

    calcVC= [[TAXViewController alloc]initWithNibName:@"TAXViewController" bundle:nil];

}else if ([viewName isEqualToString:@"Rent"]){

    calcVC= [[RENTViewController alloc]initWithNibName:@"RENTViewController" bundle:nil];

}else //continues with more views...



//Then add it to the subview

[firstView addSubview:calcVC.view];

Upvotes: 0

Views: 96

Answers (3)

xemacobra
xemacobra

Reputation: 1954

Not sure if this answers your question but you can loop through all the subviews and call it if it exists as follows:

for (UIView *subview in [self.view subviews]) {
    if ([subview respondsToSelector:@selector(resignFirstResponder)]) {
        [subview resignFirstResponder];
    }
}

Upvotes: 0

Dan F
Dan F

Reputation: 17732

You can make a basic protocol that all of your sub-view controllers implement that has does everything you need (resign first responder and anything else).

Upvotes: 1

T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6393

Not sure if I've got the gist of this, mostly because it sounds like you've already solved it yourself. :)

But, from what I can see the ViewController you are talking about is always an UIViewController instance named calcVC. If it is always this viewController's view you are referring to you can simply call [calcVC.view resignFirstResponder];

Upvotes: 1

Related Questions