Search code examples
iphoneobjective-cuitableviewios6

How to reload table of one view from other view


I am trying to reload table of MyView.m from MySliderController.m but its not reloading. i have declared below method in MyView.m

 -(void)updateData{
      [self.itablview reloadData];
 }

and calling that method form MySliderController.m class by below code

 DDMenuController *menuController = (DDMenuController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).DDmenuController;
MyView *obj = [[MyView alloc]init];

if(indexPath.row == 3){
    [menuController showRootController:YES];
    [obj updateData];
}

that method gets called but table is not reloading even i am checking existence of tableview by below code

-(void)reloadtable:(id) sender{
    NSlog(@"%@",self.itableview)
    [self.itableView reloadData];
}

in console i get (null)

but when i am checking in below code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@", self.itableView);
}

i am getting below message . it means table exists

2013-06-29 19:31:48.673 slidingViews[7025:c07] <UITableView: 0x79d9400; frame = (0 0; 320 480); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x714e940>; layer = <CALayer: 0x714e3f0>; contentOffset: {0, 0}>

what can be the issue. Please check my code here 
https://github.com/kanwarpalSingh/mycode

Solution

  • Your problem is that when you're instantiating the MyView you're actually creating a new (but not visible) version of the MyView view controller, not the original MyView you originated from. There are a few different methods to handle this and it really comes down to what works best for you.

    One example would be to subscribe MyView to a custom notification originating from MySliderControl such as this...

    MyView.m

    - (void)viewDidLoad
    {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateData:) name:@"updateDataOnMyView" object:nil];
    }
    

    MySliderControl.m

    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateDataOnMyView" object:nil];
    

    You can also use a delegate method if you don't like this setup. There are plenty of tutorials out there on creating your own delegates as well. Good luck!