Calling a method from another viewcontroller in objective c -


i have controller (name : parent.m) extending uiviewcontroller add view (controller (name: child.m) ) has table view in it.

on click of 1 of rows on table of child.m, want invoke method in parent.m class. not want create new instance of parent.m.

could explain how can accomplish this. need define protocol between two? if can explain me how.

if you're using uinavigationcontroller, can hands on parent view controller doing in child view controller.

parentviewcontroller * parentview = (parentviewcontroller *)[self.navigationcontroller.viewcontrollers lastobject];  

if not using uinavigationcontroller, use notifications (may overkill). or key value observing parent child (i.e. change display or value of based on change in child view controller).

or, straightforward thing here set parent delegate target of child.

1 )

in childviewcontroller.h file, create protocol looks this:

@protocol childviewdelegate  - (void) dosomethingwith: (nsstring *) thisstring;  @end 

2)

create delegate property in childviewcontroller (which you'll set parent view).

3)

in parentviewcontroller.h file, add "<childviewdelegate>" after uiviewcontroller @interface declaration... i.e.

@interface parentviewcontroller : uiviewcontroller <childviewdelegate> 

4)

implement "dosomethingwith:" method.

5)

and when instantiate / create child viewcontroller parent, don't forget set delegate parent.

6)

lasty, when want send message child parent, simple as:

[delegate dosomewthingwith: thisstring]; 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -