iosiphoneobjective-cipadpopviewcontroller

how to send data back with dismissViewController


i have 2 views in iPhone application. FirstViewController and MultiSelectViewController.

in FirstViewController there is a button to go to MultiSelectViewController. In MultiSelectViewController i have a tableviewcontroller to multiselect and send result back to FirstViewController with Done button

my problem is with done button. i don't know how to send data back to the FirstViewController. it has to be with dissmissviewcontroller.

this is .h file of MultiSelectViewController

@protocol MultiSelectDelegate <NSObject>
-(void) multiselectViewControllerDismissed;
@end

@interface MultiSelectViewController : UITableViewController
{
  __weak id myDelegate;
}
@property(nonatomic,retain)NSArray *myData;
@property(nonatomic, retain)NSMutableArray *selectedData;
@property (nonatomic, weak) id<MultiSelectDelegate> myDelegate;

this is my done button in .m file of MultiSelectViewController

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.allowsMultipleSelection = YES;
selectedData=[[NSMutableArray alloc] init];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                              style:UIBarButtonItemStyleBordered
                                                             target:self
                                                             action:@selector(multiselectViewControllerDismissed)];
self.navigationItem.rightBarButtonItem = barButton;

 }

and lastly here is my done button action:

-(void)multiselectViewControllerDismissed
{
   NSLog(@"%@",selectedData);

}

i don't understand how can i send data and get back in FirstViewController


Solution

  • You redefine multiselectViewControllerDismissed delegate method as

     multiselectViewControllerDismissedWithData:(NSMutableArray *)dataSelected
    

    And, in .h file of FirstViewController implement the delegate i.e.,

    @interface FirstViewController: UIViewController <MultiSelectDelegate> 
    

    and in the button action of FirstViewController.m assign delegate of MultipleSelectViewController as self. ie.,

     MultipleSelectViewController * msvc = [[MultipleSelectViewController alloc] init]; 
    msvc.myDelegate = self;
    

    and implement

     -(void)multiselectViewControllerDismissedWithData:(NSMutableArray *)dataSelected  
    

    this method in FirstViewController.m

    And, in the Done button action method of MultipleSelectViewController.m, call method multiselectViewControllerDismissedWithData with delegate i.e.,

     [self.myDelegate multiselectViewControllerDismissedWithData:selectedData];  
    

    That's it. You could now pass selectedData array from MultipleSelectViewController to FirstViewController