Pass data back with dismissViewControllerAnimated

2019-04-08 06:01发布

问题:

I have a FirstViewController and a SecondViewController. I created a button in FirstViewController in order to perform a segue modally to SecondViewController.

In SecondViewController I have a tableView showing a list of 8 items, as I select an item from that list, I dismissViewControllerAnimated:, going back to FirstViewController.

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

I used this post as a reference for my code: dismissModalViewController AND pass 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 run the app I can go from FirstViewController to SecondViewController, and when I select a row from the tableView I go back to FirstViewController just fine. The problem is that the string "SOME STRING HERE" was not passed back.

What am I missing?

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

回答1:

You have to set the delegate when you present the second view controller, ie, in your FirstViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"present_secondviewcontroller]) {
          SecondViewController *svc = (SecondViewController *)segue.destinationViewController;
          svc.delegate = self;
     }
}


回答2:

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