Reputation: 1520
I have an iCarousel view, which for those of you who don't know what it is, works a lot like UITableView or UICollectionView. If you'd like, we can pretend that I'm using one of those.
Anyways, my iCarousel has a bunch of items in it already. They're defined in a global NSMutableArray by the name of "items". My problem is when I switch views for a minute away from the carousel, when i switch back, the items are gone. I believe this is because when i switch views, the carousel is deallocated or the reference is lost. When i try to reload it, i use this code:
self.carousel = [[iCarouselExampleViewController alloc] initWithNibName:@"iCarouselExampleViewController" bundle:nil];
[self presentViewController:self.carousel animated:YES completion:nil];
[self.carousel.carousel reloadData];
This code brings me to an empty carousel. I assume this is because, as aforementioned, i lost the reference to the view and i had to reallocate it with the initWithNibName call. Anyways, how do i get around that? I dont want to lose the data that was in the class. any suggestions? And how would i go about switching back to a UITableView from a different view controller in the first place?
Upvotes: 0
Views: 475
Reputation: 1229
If you create the carousal while presenting it will deallocates the existing one and creates the new one. I don't think for your case it needs to be allocated every time you are presenting.
if(self.carousel == nil) {
self.carousel = [[iCarouselExampleViewController alloc] initWithNibName:@"iCarouselExampleViewController" bundle:nil];
}
[self presentViewController:self.carousel animated:YES completion:nil];
[self.carousel.carousel reloadData];
Upvotes: 0
Reputation: 6432
If the object that holds the items that populate your iCarousel gets deallocated you can store the data source somewhere else. You can consider using CoreData for that. Or if you don't want to have to deal with that, declare the NSMutableArray in the AppDelegate or somewhere similar that you know that won't get deallocated. Another option would be to create a Singleton for it. There are many many options.
Hope this helps!
Upvotes: 1