1

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

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
ercan
  • 825
  • 3
  • 16
  • 27

3 Answers3

3

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

  • 1
    thanks for the answer. i changed the done button's action -(void)done { if([self.myDelegate respondsToSelector:@selector(multiselectViewControllerDismissed:)]) { [self.myDelegate multiselectViewControllerDismissed:selectedData]; } [self dismissViewControllerAnimated:YES completion:nil]; // NSLog(@"%@",selectedData); } however i can send data back to the FirstViewController i cannot dismiss page. I mean it stay in MultiSelectViewController – ercan Jun 30 '14 at 07:38
0

Two standard ways of passing data in obj-c:

  1. Use references and assign manually. In your example, the first view controller passes a reference of itself to the second view controller. The second view controller assigns a designated property with the required data using the reference.
  2. The publisher-subscriber pattern using LocalNotifications. The first view controller listens for a particular location notification, and the second view controller, before being dismissed, does a broadcast with the data.

I'd recommend the 1st approach for your example. Some sample code:

In the .h file:

@interface FirstViewController:UIViewController

@property NSMutableArray *receivedData; //property to receive selected data

@end

In the .m file:

MultiSelectViewController *msvc = [MultiSelectViewController alloc] init];
msvc.presentingViewController = self; // pass reference of 1st VC to 2nd VC
[self presentViewController:msvc animated:YES];

In MultiSelectViewController.h file:

#import "FirstViewController.h"

@interface MultiSelectViewController: UITableViewController
...

@property FirstViewController *presentingViewController;
...
@end

In MultiSelectViewController.m file:

-(void)multiselectViewControllerDismissed
{
   NSLog(@"%@",selectedData);
   presentingViewController.receivedData = selectedData;
}
Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
0

first create your delegate method as

-(void)dismiss:(NSString *)str;

while did select get a value from based on indexpath.row and store it as nsstring.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


   self.valueString=[NSString stringWithFormat:@"%@",[self.arrayValue objectAtIndex:indexPath.row]];
    NSLog(@"%d",indexPath.row);

   }

done button click:

call your delegate method like

[self. myDelegate dismiss:"your string which you get from table"]
 [[self navigationController] popViewControllerAnimated:YES];

and in your first view controller…

import the view controller class and call delegate method..

create instance for second view controller...

 MultiSelectViewController *txtNext=[[MultiSelectViewController alloc]init];
  txtNext. myDelegate =self;

then

-(void)dismiss:(NSString *)str
{

NSString *strng=str;
nslog("%@",strng);
}
karthikeyan
  • 3,821
  • 3
  • 22
  • 45