0

I have a view controller which displays a carousel control (iCarousel). The view is rendered correctly and the carousel is displayed. Right after that a modal is displayed which allows the user to agree to certain terms. I want that once they agree I refresh the viewcontroller which contains the carousel control. Basically, I want to rotate the carousel to some random index.

- (IBAction)accept:(id)sender
{
    NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsStoryboard"];

    [newsViewController loadNews];  
    [newsViewController.view setNeedsDisplay]; 



    [self dismissViewControllerAnimated:YES completion:nil];
}

The above code does call the loadNews and fetches it but the view is never refreshed.

john doe
  • 9,220
  • 23
  • 91
  • 167
  • 1
    You need create a delegate for the modally presented viewController. You can refer this post http://stackoverflow.com/a/9736559/767730 – Anupdas Apr 16 '13 at 16:53
  • 1
    It's not working because you're making a new NewsViewController, not calling loadNews on your existing one. You will probably want to make a delegate or use a notification. See http://stackoverflow.com/questions/569940/whats-the-best-way-to-communicate-between-view-controllers – Aaron Brager Apr 16 '13 at 16:54
  • It is not the newscontroller that is displaying the modal that is why it is hard to create a delegate based solution. Maybe I will see NSNotification – john doe Apr 16 '13 at 17:01
  • NSNotification did the trick! Thanks everyone! – john doe Apr 16 '13 at 17:07

2 Answers2

2

What happens to the carousel should really be up to the view controller that manages it, not the modal view controller. Make the modal controller do its thing and return whatever data it collects to its parent. The parent (in this case, the carousel's controller) can then look at that data and decide what it needs to do next (refresh, for example).

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

The problem is this line:

    NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsStoryboard"];

That is not the old view controller; it is a new, unused copy of that view controller. You need to create a line of communication from the modal view controller back to the existing view controller.

The typical way to do this is through a delegate, which you set when creating the modal view controller. If you look at the Xcode Utility template, you will see that it illustrates this architecture. The original view controller sets itself as the modal view controller's delegate, and the modal view controller is thus able to talk back to the original view controller as it is dismissed.

This is such an important thing to be able to do that I talk about it at length in my book:

http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller

matt
  • 515,959
  • 87
  • 875
  • 1,141