0

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?

4

2 回答 2

1

如果包含填充 iCarousel 的项目的对象被释放,您可以将数据源存储在其他位置。您可以考虑为此使用 CoreData。或者,如果您不想处理这个问题,请在 AppDelegate 或您知道不会被释放的类似地方声明 NSMutableArray。另一种选择是为它创建一个单例。有很多选择。

希望这可以帮助!

于 2013-09-18T17:28:49.600 回答
0

如果您在演示时创建轮播,它将取消分配现有的并创建新的。对于您的情况,我认为不需要在您每次演示时都分配它。

if(self.carousel == nil) {
   self.carousel = [[iCarouselExampleViewController alloc]   initWithNibName:@"iCarouselExampleViewController" bundle:nil];
}
[self presentViewController:self.carousel animated:YES completion:nil];
[self.carousel.carousel reloadData];
于 2013-09-18T17:34:30.673 回答