0

hi i am working on an app and all was going good till now.... i am stuck at this point.. here is my storyboard snapshot..

storyboard

in the DemoTableViewController when i clock on "filters" button .. Brands TableViewController is opened modally . after user select multiple rows in Brands TableViewController ,, he then clicks on done button and view controller is dismissed by this code:

-(IBAction)DonePressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

I am storing the user selections in NSMutableSet and it is to be Used in DemoTableViewController so that i can reload the table rows according to the selection but i dont know how to send NSMutableSet to DemoTableViewController and then reload the table according to selection.. what is the right way to dismiss modal view and reload the parent DemoTableViewController i know i am not doing it correctly but can anyone help me in doing it... here is some code --- DemoTVC.h

#import <UIKit/UIKit.h>
#import "MyModelClass.h"
#import "brandsTableViewController.h"

 @interface demoTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate,MyModelProtocol,FilterProtocol>
@property (strong, nonatomic) IBOutlet UITableView *demoTable;
- (IBAction)FilterPressed:(id)sender;
@end

DemoTVC.m-- the method which performs segue--

- (IBAction)FilterPressed:(id)sender {
    [self performSegueWithIdentifier:@"FilterPressed" sender:self];
 }

the delegate method which is called to get the values from BrandsTVC--

-(void)GetTheSet:(NSMutableSet *)MySet{
   [self dismissViewControllerAnimated:YES completion:nil];
}

viewdidLoad--

- (void)viewDidLoad
 {
    [super viewDidLoad];
    productArray = [[NSMutableArray alloc] init];
    homeModel = [[MyModelClass alloc] init];
    // Set this view controller object as the delegate for the home model object
    homeModel.delegate = self;
    // Call the download items method of the home model object
    [homeModel downloadItemswithurl:@"url is written here"];  
}

BrandsTVC.h---

#import <UIKit/UIKit.h>
#import "MyModelClass.h"

@protocol FilterProtocol <NSObject>
-(void)GetTheSet:(NSMutableSet *) MySet;
@end

@interface brandsTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate,MyModelProtocol>

@property (strong, nonatomic) IBOutlet UITableView *RetailerList;
@property (strong,nonatomic) NSMutableSet *selectStates;
@property (nonatomic, weak) id<FilterProtocol> delegate;
- (IBAction) DonePressed:(id)sender;
@end

BrandsTVC.m---

@interface brandsTableViewController ()
{
    MyModelClass *myModelClass;
    NSMutableArray *BrandsArray;
    brandsTableViewController *Delegate;
}
 @end

viewDidLoad----

- (void)viewDidLoad
{
    [super viewDidLoad];

    BrandsArray = [[NSMutableArray alloc] init];
    self.selectStates=[NSMutableSet new];
    myModelClass = [[MyModelClass alloc] init];
    // Set this view controller object as the delegate for the home model object
    myModelClass.delegate = self;
    // Call the download items method of the home model object
    [myModelClass downloadItemswithurl:@"url to get json"];
    self.tableView.allowsMultipleSelection = YES;
}

Done Button is called ---

- (IBAction)DonePressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    id<FilterProtocol> strongDelegate = self.delegate;

    if ([strongDelegate respondsToSelector:@selector(GetTheSet:)]) {
            [strongDelegate GetTheSet:self.selectStates];
    }  
}
@end
  • The proper way is to use delegate. Your DemeTVC is the delegate of BrandTVC. when doneButtonPressed called in BrandTVC, send the selected Brands to the delegate. The delegate then dismissViewController. – John Aug 03 '14 at 08:03
  • http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers – John Aug 03 '14 at 08:03
  • i have tried exactly the same but delegate method is not called in after the dismissal of modal view... – Jaspreet Singh Aug 03 '14 at 08:12
  • doneButtonPressed should be implemented in BrandTVC, which calls the delegate like [self.delegate didSelectBrand.....], BrandTVC not dismissing the modal. dismissViewController should be called in the didSelectBrand method in DemoTVC. – John Aug 03 '14 at 09:19
  • ohh now i am gtting the point.. i will try it and reply – Jaspreet Singh Aug 03 '14 at 09:22
  • i cant get it working – Jaspreet Singh Aug 03 '14 at 13:02
  • Post your code of your new try. Did you set the delegate of destinationViewController in prepareForSegue? – John Aug 03 '14 at 19:39
  • Hey I have added some code.... take a look at it.. and GetTheSet method in DemoTVC is not being called on pressing Done button – Jaspreet Singh Aug 03 '14 at 20:38
  • You did not set the BrandTVC's delegate to be the DemoTVC. You should set it in prepareForSegue – John Aug 03 '14 at 21:17
  • i cant figure out how to do that..!! – Jaspreet Singh Aug 04 '14 at 08:10

2 Answers2

0

You can keep those elements in NSUserDefaults, and whenever DemoTableViewController appears, you send [tableView reloadData]. Do this in the viewDidAppear: method.

You can also use Core Data, but there are a lot of details to explain how to do that.

ppalancica
  • 4,236
  • 4
  • 27
  • 42
  • the problem is i must go back to demoTableViewController and reconnect to server and fetch the contents of selected rows/retailers in modal view... initially on first loading ,, demo View is showing content from all Retailers – Jaspreet Singh Aug 03 '14 at 07:44
  • In the demoTableViewController, when you present the the TableViewController, do like this: [self presentViewController:demoVC animated:YES completion:^{ // code to reload data from server... // also call [tableView reloadData]; when new data is downloaded... }] – ppalancica Aug 03 '14 at 17:13
0

After a lot of trial and error i come to know that in BrandsTVC--

- (IBAction)DonePressed:(id)sender {

    NSLog(@"done pressed %@",self.delegate);
    [delegate GetTheSet:self.selectStates];
}

self.delegate is giving me NULL. delegate is not setup and i cant figure out how to do that.. someone help me..

this answer solved my problem :D custom viewcontroller delegate not working

Community
  • 1
  • 1