-1

I am struggling on calling method from viewController B to viewController A. Need to call viewController B close button click to Dismissviewcontroller then immediately need to call one method and want to pass two string values on viewController A. Its like reverse process.

FYI : I am using Storyboard and present viewController for B. A is the mainviewcontroller.

Tj3n
  • 9,837
  • 2
  • 24
  • 35
Sanju
  • 129
  • 1
  • 10

4 Answers4

0
  • use viewWillAppear in controller A.

  • Post a notification from controller B and add observer on controller A.

Post notification on controller B close button [[NSNotificationCenter defaultCenter] postNotificationName:"NAME" object:nil userInfo:nil];

Add observer on controller A:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(perform:) name:@"NAME" object:nil];

  • Implement delegates on controller B and implement it on controller A so once you click on close button controller B just call the delegate and perform what ever you want.
  • Implement KVO
Buntylm
  • 7,345
  • 1
  • 31
  • 51
0

There are many option for passing values

  1. Use protocol and delegates
  2. Use unwind segues
  3. Use Notifications

there are many other option to do the same google on above points you will get tons of demos for this.

hmlasnk
  • 1,160
  • 1
  • 14
  • 33
Milap Kundalia
  • 1,566
  • 1
  • 16
  • 24
0

well the easiest way but not the most efficient is to make a global object of the viewController A and viewController A view did load method call that global variable and make it equal to self and in then the dismiss from viewController B

will use

[self dismissViewControllerAnimated:YES completion:^{


  // here you can create a code for calling the global **viewController A** object to call the function you need

}];

conclusion :

in viewController A header file :

extern A* AGlobalInstance;

and in A.m file just below the #import "A.h"

A* AGlobalInstance;

and in the viewDidLoad

- (void)viewDidLoad {
  [super viewDidLoad];
  AGlobalInstance = self;
}

then in B.m button just use

[self dismissViewControllerAnimated:YES completion:^{
    [AGlobalInstance function];
 }];

but you must go to A viewController first before going to B to make it work

Mostafa Sultan
  • 2,268
  • 2
  • 20
  • 36
0

As the above answers suggested you to use Delegates and Notification so I am not gonna suggest you those. Apart from them I would like to ask you to go for Block.

typedef void(^valueHandler)(id anyObject1, id anyObject2);

@viewController A

B *instanceOfB;
[instanceOfB setValueHandlerBlock^(id anyObject1, id anyObject2) {
// Here you can receive the values.
}];

@viewController B

@property (nonatomic, copy) valueHandler valueHandlerBlock;

//To pass the value
if (valueHandlerBlock) {
    valueHandlerBlock(@"a String value", anArray);
    // When ever the above line will execute it will pass the values to view controller A.
}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
  • are you sure he won't need to set `instanceOfB` to equal self in B view controller first , to connect them together ? – Mostafa Sultan Jan 05 '16 at 05:35
  • 1
    I think you should go through https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html this first. – Tapas Pal Jan 05 '16 at 05:53
  • typedef where I want to put?@Tapas Pal – Sanju Jan 05 '16 at 13:06
  • @Sanju you can add anywhere I mean which will be global accessible. But in this case you can add at the top of `@viewController B` interface in .h file. – Tapas Pal Jan 06 '16 at 05:41