-1

I want to pass data on Dismiss View controllerB To View controllerA and pass two text field value to View controllerABelow posted code not working for me!

ViewController B

ClassB.h

#import <UIKit/UIKit.h>

@protocol SecondDelegate <NSObject>
-(void) secondViewControllerDismissed:(NSString *)stringForFirst
@end


@interface SecondViewController : UIViewController
{
    id                              myDelegate; 
}

@property (nonatomic, assign) id<SecondDelegate>    myDelegate;

ClassB.m

@synthesize myDelegate;

//Below Code Into my button Click

if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
{
    [self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!"];
}
[self dismissModalViewControllerAnimated:YES];

ViewController A

ClassA.h

#import "SecondViewController.h"

@interface FirstViewController:UIViewController <SecondDelegate>

ClassA.m

Now when you instantiate secondViewController in firstViewController you should do the following:

// Here I am using below Code
    ClassB *BViewController;
    [self.storyboard instantiateViewControllerWithIdentifier:@"BViewController"];
    [self presentViewController:BViewController animated:YES completion:nil];



- (void)secondViewControllerDismissed:(NSString *)stringForFirst
{
    NSString *thisIsTheDesiredString = stringForFirst; //And there you have it

}

Sanju
  • 129
  • 1
  • 10

2 Answers2

-1
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)

func methodOfReceivedNotification(notification: NSNotification){
    //Take Action on Notification
}

Add This observer to A ViewController and Create Method.

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil, userInfo:dataDict)

On Button click "post this Notification" from B ViewController. This Will pass controller to "A ViewController" "methodOfReceivedNotification" Method along with "dataDict".

nadim
  • 776
  • 1
  • 12
  • 26
-1

You need to use delegate protocols... Here's how to do it:

Declare a protocol in your secondViewController's header file. It should look like this:

#import <UIKit/UIKit.h>

@protocol SecondDelegate <NSObject>
-(void) secondViewControllerDismissed:(NSString *)stringForFirst
@end

@interface SecondViewController : UIViewController
{
    id                              myDelegate; 
}

@property (nonatomic, assign) id<SecondDelegate>    myDelegate;

Dont forget to synthesize the myDelegate in your .m file:

@synthesize myDelegate;

In your firstViewController's header file subscribe to the SecondDelegate protocol by doing this:

#import "SecondViewController.h"

@interface FirstViewController:UIViewController <SecondDelegate>

Now when you instantiate secondViewController in firstViewController you should do the following:

SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

second.myString = @"This text is passed from firstViewController!";

second.myDelegate = self;

second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

[self presentModalViewController:second animated:YES];

[second release];

Lastly, in the .m file for your first view controller implement the SecondDelegate's method for secondViewControllerDismissed:

- (void)secondViewControllerDismissed:(NSString *)stringForFirst
{
    NSString *thisIsTheDesiredString = stringForFirst; //And there you have it.....
}

Now when you're about to dismiss the second view controller you want to invoke the method implemented in the first view controller. This part is simple. All you do is, in your second view controller, add some code before the dismiss code:

    if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)])
{
    [self.myDelegate secondViewControllerDismissed:@"THIS IS THE STRING TO SEND!!!"];
}
[self dismissModalViewControllerAnimated:YES];

Delegate protocols are EXTREMELY, EXTREMELY, EXTREMELY useful. It would do you good to familiarize yourself with them :)