-2

I have two view controllers. First one has a table view and using the second view controller I'm calling a method on first view controller. In that method I'm trying to adding an item to the objects array and refresh the table view.

[postsArr insertObject:post atIndex:postsArr.count];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.newsTableView reloadData];
});

I'm calling this method in 2nd view controller,

dispatch_async(dispatch_get_main_queue(), ^{
    [self.navigationController popViewControllerAnimated:YES];
    [self.appContext.creator createWithText:userText  completion:^(UserData *userData,NSError *error) {
        if (error == nil){
            if (userData != nil) {
                [self.parent addNew:((UserData*)[userData.list objectAtIndex:0]) withImage:nil];
            }
        }else{
            [self showAlertWith:@"Error" with:@"Error occurred!"];
        }
    }];
});

How may I refresh the table view from another view controller?

codebot
  • 2,540
  • 3
  • 38
  • 89

3 Answers3

3

Add this on the top of the interface of your second viewcontroller

@protocol SecondViewControllerDelegate <NSObject>
 - (void)addNewItem:(id)item;
@end


@interface SecondViewController : UIViewController
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;

@end

The point in your firstViewController from where you are instantiating your secondViewController for navigation add secondviewController.delegate as self.

self.secondViewController.delegate = self;

From the point where you get response in your secondViewController and you want to addItem in your firstViewController call delegate method from here and pass that item to firstViewController in that delegate method.

dispatch_async(dispatch_get_main_queue(), ^{
    [self.navigationController popViewControllerAnimated:YES];
    [self.appContext.creator createWithText:userText  completion:^(UserData *userData,NSError *error) {
        if (error == nil){
            if (userData != nil) {
                [self.delegate addNewItem:((UserData*)[userData.list objectAtIndex:0]) withImage:nil];
            }
        }else{
            [self showAlertWith:@"Error" with:@"Error occurred!"];
        }
    }];
});

Add the implementation of addNewItem in your firstViewController

- (void)addNewItem:(id)item{
    [postsArr insertObject:post atIndex:postsArr.count];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.newsTableView reloadData];
    });
}
Shehzad Ali
  • 1,846
  • 10
  • 16
1

You can use NSNotificationCenter

Fire notification when you need to reload the table

Just follow the link to know how implement notifications

Send and receive messages through NSNotificationCenter in Objective-C?

Community
  • 1
  • 1
Mandeep Kumar
  • 776
  • 4
  • 18
  • Notifications Are Heavy To Handle .... So try using keyvalue observer first then delegates and the NSnotification and the method i User Below is quite easy to implement and not as heavy as above methods . – Himan Dhawan Feb 16 '16 at 10:51
0

There Are Many methods through you can achieve your Goal

  1. NSNotification
  2. Delegates
  3. KeyValueObserver

But I found most reliable is

@property (copy) void (^valueTypeChangedBlock) (NSarray arrayTypeObject);

Add This Property In .h file

And In .m File Add This

self.valueTypeChangedBlock = ^(NSarray NewArr) {
        postsArr = NewArr
        [self.newsTableView reloadData];
    };

Where Ever U want to change the array from which table view Reload Add the new array here

self.valueTypeChangedBlock (self.NewArray);
Himan Dhawan
  • 894
  • 5
  • 23