Passing data back using rejectViewControllerAnimated

I have FirstViewController and a SecondViewController . I created a button in the FirstViewController in order to execute the modally segment before the SecondViewController .

In SecondViewController I have a tableView displaying a list of 8 elements, since I select an element from this list, I dismissViewControllerAnimated: returning to FirstViewController .

What I would like to do is pass the string back to FirstViewController .

I used this post as a reference for my code: rejectModalViewController AND pass the data back

So this is what I have:

in FirstViewController.h

 #import <UIKit/UIKit.h> #import "AppDelegate.h" #import "SecondViewController.h" @interface FirstViewController : UIViewController <SecondDelegate> @end 

in FirstViewController.m

 #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController ... - (void)secondViewControllerDismissed:(NSString *)stringForFirst { NSString *theString = stringForFirst; NSLog(@"String received at FirstVC: %@",theString); } @end 

in SecondViewController.h

 #import <UIKit/UIKit.h> @protocol SecondDelegate <NSObject> -(void) secondViewControllerDismissed:(NSString *)stringForFirst; @end @interface SecondViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> { __weak id myDelegate; } @property (nonatomic, weak) id<SecondDelegate> myDelegate; @property (weak, nonatomic) IBOutlet UITableView *myTableView; @end 

in SecondViewController.m

 #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController @synthesize myDelegate; @synthesize myTableView; ... - (int)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 8; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [atributoTableView dequeueReusableCellWithIdentifier:@"MainCell"]; if(cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"]; } cell.textLabel.text = //strings from an array here; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if([self.myDelegate respondsToSelector:@selector(secondViewControllerDismissed:)]) { [self.myDelegate secondViewControllerDismissed:@"SOME STRING HERE"]; NSLog(@"string passed"); } [self dismissViewControllerAnimated:YES completion:nil]; NSLog(@"SecondViewController dismissed"); } @end 

When I launch the application, I can switch from FirstViewController to SecondViewController , and when I select a row from the View table, I return to FirstViewController just fine. The problem is that the string β€œSOME LINE HERE” was not returned.

What am I missing?

By the way, I'm not sure if this is relevant: I use ARC and Storyboards.

+5
ios objective-c delegates uistoryboard
Mar 08 '13 at 19:55
source share
2 answers

You must set the delegate when you present the second view controller, i.e. in your FirstViewController:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"present_secondviewcontroller]) { SecondViewController *svc = (SecondViewController *)segue.destinationViewController; svc.delegate = self; } } 
+4
Mar 08 '13 at 20:19
source share

According to your problem and the above solution, you should add svc.myDelegate = self; instead of svc.delegate = self;

0
Jun 01. '15 at 21:27
source share



All Articles