开发者

update favourite list in realtime iphone

开发者 https://www.devze.com 2023-04-09 20:28 出处:网络
My app has two tabs, one of which is a table view, in the other tab I can add the current object to the core data storage, and I wish the table view could be up-to-date anytime I am done adding and sw

My app has two tabs, one of which is a table view, in the other tab I can add the current object to the core data storage, and I wish the table view could be up-to-date anytime I am done adding and switch back to that tab (table view). Currently, the table view only refreshes when my app relaunches which is understandable, because I am getting the data in the viewDidLoad method in that viewController. When I switch back and forth between these two tabs, their views are already loaded, so how can I update the table view in realtime? Any advice would be appreciated.

Update:

A good example is the contact app on iphone, but I don't know how to do that...


Here is some code in the table view controller.

- (void)viewDidLoad {
    [super viewDidLoad];

    bList = [[NSMutableArray alloc] init];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

      //some code to get data from core data storage and put them in the bList array.

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveAddNotification:) 
                                             name:@"AddNotification"
                                           object:nil];
}

-(void) receiveAddNotification: (NSNotification *)notification{
    if ([[notification name] isEqualToString:@"AddNotification"]){
        NSLog (@"Successfully received the add notification!");
 开发者_高级运维   }
}

Code in the other tab's view controller.

-(IBAction) addSomething {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddNotification" object:self];
        //some code to store an object in core data.
}

The console can output the message "Successfully received the add notification!", which means the notification works fine, but the table view didn't get refreshed when I switch back to the table view tab from edit tab. I'm assuming the newly added object in addSomething method should be updated to the receiveAddNotification: method either.


Hi Michael as I understand your question, You can use notifications concept. Start notification service in the table view controller and when you are done with adding the things you can post the notification so that, that will update the table view automatically.

Look in to the NSNotificationCenter.

Let me explain with an example.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveTestNotification:) 
                                             name:@"TestNotification"
                                           object:nil];

add the above statement in the table view controller. Then you just define the "receiveTestNotification:" function with [tableview reload];

And in the editing view controller when user taps the done button you post the notification

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

So it will call the "receiveTestNotification:" method and run the code what ever you gave in that method.

0

精彩评论

暂无评论...
验证码 换一张
取 消