0

I have a button in mainviewcontroller, when that button is tapped I want to call a method on another view controller. Here below is an image showing my scenario, Green button click to call preview controller VC1 received method without navigation anything. I just need to call that method only!

enter image description here

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
Sanju
  • 129
  • 1
  • 10

2 Answers2

0
  • Declare a method in .h file of VC1 and implement in .m file of VC1
  • Call that method from MainView Controller ,
  • Create a NSNotificationObserver in MainView Controller and implement it in .m file of MainView Controller ,

Now when method of VC1 is called and before it returns, fire a PostNotification from VC1 and pass the parameter which you want in MainView Controller.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
Vatsal Raval
  • 311
  • 1
  • 9
0

@RobAu has given correct answer as you can use NSNotificationObserver and call from anywhere.

Other alternate which i have used is using protocol and is very simple.

Just declare a protocol in the child controller. while launching the view put the parent controller as delegate. now call the delegate method from where ever you want from child controller

Here is example which i used (In my case all the child controller was of same kind)

//ChildClass.h file of child class

@protocol updateIndex <NSObject>
    -(void)updateMediaId:(NSString*)currentMediaId;
@end

@interface ChildClass : UIViewController
    @property NSString *imageID;
    @property id updateIndexDelegate;
@end

//ChildClass.m file for child class where you want to call the delegate method

(I called in viewDidAppear method)

-(void)viewDidAppear:(BOOL)animated{
    [self.updateIndexDelegate updateMediaId:_imageID];
}

And in ParentClass.m file of parent class use the delegate to self like this

ChildClass *childObject = [[ChildClass alloc] init];
childObject.delegate = self;

and define the delegate method like this

-(void)updateMediaId:(NSString*)currentMediaId {
    NSLog(@"%@",currentMediaId);
}

Enjoy coding

Iftikhar Ali Ansari
  • 1,650
  • 1
  • 17
  • 27
  • Ok Now I will try ur code, Once I got I will upvote soon! Thank you @lftikhar – Sanju Jan 20 '16 at 04:36
  • Hi @lftikhar where I want to maintain ChildClass *childObject = [[ChildClass alloc] init]; childObject.delegate = self; this code into parent file – Sanju Jan 20 '16 at 04:45
  • above method is not working. Can you tel any thing solution. Actually above Image first view controller button click to call child viewcontroller method. Exactly I need that one. Pleas ehelp me! – Sanju Jan 20 '16 at 05:22
  • where ever you are allocating instance of child view controller just add the delegate to self.(This will be in parent class) – Iftikhar Ali Ansari Jan 20 '16 at 08:24
  • Cal I call directly like button click to call VC1 method ?@lftikhar – Sanju Jan 20 '16 at 09:18
  • Great Answer! but [this may be a little more descriptive](http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers) – LuAndre Feb 23 '16 at 15:53