3

I am developing an iPad application using storyboard. In my application I have connected one modal view controller from first view controller using segue modal presentation.The modal view is dismiss by click one button appear in the modal view.How can i pass one dictionary from modal view controller to first view controller without using segue.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nithin U S
  • 100
  • 1
  • 9

5 Answers5

5

You can do passing value using Notification.

// Send Data    
   NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                              anObject, @"objectName", 
                              anotherObject, @"objectId",
                              nil] autorelease];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary];

You can retrieve the dictionary from the inbound notification that you observe. Add the observer in advance of posting the notification.

//this might be in your init method or a viewDidLoad method of your FirstViewController

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil];
enter code here

// for get data

-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}

note that you should remove your object as an observer in the dealloc method.

[[NSNotificationCenter defaultCenter] removeObserver:self]
Sabareesh
  • 3,585
  • 2
  • 24
  • 42
1

Using delegate its possible. Go through this

Passing Data between View Controllers

its has easy and perfect answers.

Community
  • 1
  • 1
Ramdhas
  • 1,765
  • 1
  • 18
  • 26
0

You can create a Segue not bounded to a button by ctrl+drag from the first controller to the second one (don't forget to give this segue and identifier).

Next In the IBAction of the button (set via Interface Builder or via addTarget:self action:forControlEvents: ) you can call the [self performSegueWithIdentifier:@"YourSegueIdentifier" sender:button];

You can pass the data to the second controller, as usual, in - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

pankaj asudani
  • 862
  • 6
  • 18
0
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {

SecondViewController *secVC = (SecondViewController*)toViewController;
secVC.property = self.property

}
Rajesh
  • 10,318
  • 16
  • 44
  • 64
0

Using Protocols is the best process for sending information to previous controller or any other controller

In your viewcontrooler where you presented write this in .h

@class yourclassName;

typedef void (^ yourclassName Callback) (NSDictionary*);

@protocol yourclassName Delegate @required - (void) yourclassNamedidReceiveData:(NSDictionary *)dataDict;

@end

in .m file

@synthesize delegate=_delegate;
@synthesize callbackBlock=_callbackBlock;

In some button action where you need to pass information back over there write this

[self yourclassNamedidReceiveData:yourDictHere]; // you can use any as per your requirement

Note: yourclassNamedidReceiveData method is declared in one class and now it is implemented in another class from where you will call this class

In your previous Controller(where you tried to present a class (suppose classA and classB ) first class is classA and presented class is classB.) in ClassA

- (void) yourclassNamedidReceiveData:(NSDictionary *)dataDict
{

}

Note* Do not forget to set delegate to classB EX:classBObject.delegate=self;
Hope this will help

Charan Giri
  • 1,097
  • 1
  • 9
  • 15
  • ..I tried the method you mentioned above,but i got 3 errors. synthesize delegate=_delegate; synthesize callbackBlock=_callbackBlock; error:-property implementation must be in interface 'MyclassName'. Then one error for at the place of call the function ..[self yourclassNamedidReceiveData:yourDictHere]; error - no visible at interface for 'MyclassName' declare the selector 'MyclassNamedidReceiveData'.Please help me how can i solve this error. – Nithin U S Apr 08 '14 at 09:50