Search code examples
objective-cios6delegatesmodalviewcontroller

pass value from presentedViewController back to presentingViewController


Making APP for IOS 6 - I have a mainViewController that when tap on button a new modalViewController is presented. That modalViewController has a tableView. When user taps on a cell I want to take a value of that cell and assign it to variable on mainViewController.

The problem is that I always get NULL when I output it in viewDidAppear method.

This is delegate method in modalViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSMutableString *event_id = [[self.events valueForKey:@"event_id"] objectAtIndex:indexPath.row];
    NSLog(@"EVENT ID = %@", event_id); //This gives me actuall ID


    [self.presentingViewController dismissViewControllerAnimated:YES completion:^{
        [self.mainViewController setEventID:event_id];
    }];
}

If you see I use completion block to set it. In my mainViewController I have a property of eventID. In my modalViewController i also have

@class MainViewController

@interface ModalViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
    IBOutlet UITableView *eventsTable;
}

@property (strong, nonatomic) MainViewController *mainViewController;

@end

For some reason I am getting eventID always NULL when I try to output it in viewDidAppear but it does output it when I first set it. What am I missing there??

Any help would be very appreciated.

EDIT:

In my MainViewController I already have other delegates set like this:

@interface MainViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate, CLLocationManagerDelegate>

Using protocol delegates approach, how would I add my custom delegate declaration there like ModalViewControllerDelegate.


Solution

  • Add a protocol/delegate to your ModalViewController and use that to communicate back to the MainViewController by subscribing to that delegate. Something like the following. (Not tested but gives you the idea). Be sure to set your delegate when you present your modal view like so.

    Study this link if you are still having issues

    ModalViewController *modal = (ModalViewController *)[[ModalViewController alloc]init];
    modal.delegate = self;
    [self presentModalViewController: modal animated:YES];
    

    ModalViewController.h

    @protocol ModalViewControllerDelegate <NSObject>
    -(void)setEventID:(id)event_ID;
    @end
    
    @property (nonatomic, weak) id<ModalViewControllerDelegate> delegate;
    

    ModalViewController.m

    [_delegate setEventID:event_ID];
    

    MainViewController.h

    #import "ModalViewController.h"
    
    MainViewController @interface ModalViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, ModalViewControllerDelegate>
    

    MainViewController.m

    -(void)setEventID:(id)event_id
    {
        [self setEventID:event_id];
    }