0

I have a modalViewController that comes up over the top of a viewController with a tableView. When the user clicks a button on the modalViewController I want to reload the tableView within the viewController with this:

[tableView1 reloadData]; 

I do not want to put the reload in the viewDidAppear or viewWillAppear methods as they get called when i do not need the tableView to reload (i.e. when the user clicks the back button to return to the tableView).

Is there a way to do this?

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
Brandon
  • 2,163
  • 6
  • 40
  • 64

6 Answers6

2

Try

1) write one method which reloads the table data.

2) Call it on the back button clicked.

Girish
  • 4,692
  • 4
  • 35
  • 55
  • Hi Girish, I have the method built. I want to call it when the user clicks a button on a modal view, not the back button. Do you know how to go about doing this? – Brandon Mar 04 '13 at 05:34
  • i believe Girish means create your reload method inside the viewcontroller (that has your tableview) and call that from the modalviewcontroller – John Riselvato Mar 04 '13 at 05:36
  • In that case create one delegate which automatically calls when you clicked the button. – Girish Mar 04 '13 at 05:36
  • @JohnRiselvato you are right but if he did not want this, then used delegate as I mentioned in previous comment. – Girish Mar 04 '13 at 05:37
  • @JohnRiselvato I guess I just don't know how to call a method on one viewController from another – Brandon Mar 04 '13 at 05:41
  • @Brandon call the method using the object of view controller as [vc reloadTableDate]; as vc is the obj of view controller. – Girish Mar 04 '13 at 05:47
2

This is the classic delegate pattern problem, in your modal view controller you need a delegate reference to the current view controller presenting it

//Modal
@protocol ModalVCDelegate
- (void)tappedBackButton;
@end

@class ModalVC: UIViewController
@property id<ModalVCDelegate> delegate;
@end

@implementation
- (void)backButtonTapped:(id)sender
{
    if (self.delegate) 
        [self.delegate tappedBackButton];
}
@end

Now, in your presenting VC, just process this delegate message

//Parent VC
- (void)showModal
{
    ModalVC *vc = [ModalVC new]; 
    vc.delegate = self;
    //push
}

- (void)tappedBackButton
{
    [self.tableView reloadData];
    //close modal
}
Dmitry Shevchenko
  • 31,814
  • 10
  • 56
  • 62
1

You can use delegate . If find it more harder then alternative is to use NSNotificationCenter. You can see accepted answer for Refreshing TableView. This is really very short, easy and understandable way.

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

Try to use this one

Make a Button and click on this button and than you can reload your data. This button make custom and use it on background.

  - (IBAction)reloadData:(id)sender
    {
         [tblView reloadData];
    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

using Notification like bellow Method:-

Create NSNotificationCenter at yourViewController's ViewdidLoad Mehod

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(ReloadDataFunction:)
                                                     name:@"refresh"
                                                   object:nil];

  [super viewDidLoad];

}
-(void)ReloadDataFunction:(NSNotification *)notification {

    [yourTableView reloadData];

}

Now you can Call this Notification from your modelViewController BackButton or else you want from calling this Refresh notification like putting this line of code:-

[[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:self];

NOTE: postNotificationName:@"refresh" this is a key of particular Notification

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
-2
You can use NSNotification to refresh table on ViewController.

Inside viewController :

-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

Write code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(reloadMainTable:) 
        name:@"ReloadTable"
        object:nil];




- (void) reloadMainTable:(NSNotification *) notification
{
  [tableView reload];
}


Inside ModelViewController:
[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"ReloadTable" 
        object:nil];

Here you can also send custom object instead of nil parameter. But be care full about removal of NSNotification observer.
Sudesh Kumar
  • 569
  • 3
  • 13