I’m making my slot machine app using iCarousel, my iCarousel contains images from NSDocumentDirectory this images was from my ImagePicker. So here’s how my app works, when the user press a button the iCarousel spins.
When it stops, display the item for 3 seconds, then deletes it.
My problem is when I go to another View, the deleted index/item is there again. How to maintain my array even I go to different views. That the deleted index/item will not be shown, only until the app was restarted, like saving an array. Thanks for the help.
// my array
- (void)viewDidLoad
{
self.images = [NSMutableArray new];
for(int i = 0; i <= 100; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png", i]];
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
[images addObject:[UIImage imageWithContentsOfFile:savedImagePath]];
}
}
}
- (void)viewWillAppear:(BOOL)animated {
spinButton = [UIButton buttonWithType:UIButtonTypeCustom];
[spinButton addTarget:self action:@selector(spin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:spinButton];
}
- (void) carouselDidEndScrollingAnimation:(iCarousel *)carousel{
[NSTimer scheduledTimerWithTimeInterval:0.56 //this arranges the duration of the scroll
target:self
selector:@selector(deleteItem)
userInfo:nil
repeats:NO];
}
// spin and delete method
- (void)spin {
[carousel scrollByNumberOfItems:-35 duration:10.7550f];
}
-(void) deleteItem {
//Removes the object chosen
NSInteger index = carousel.currentItemIndex;
[carousel removeItemAtIndex:index animated:YES];
[images removeObjectAtIndex:index];
}
What I need is, when the index/item is deleted, it will not be shown temporarily even if I go to other views. The views will only be restarted after app is closed and open again
Your problem is your are creating the images
NSMutableArrayevery time you enter the view.As @Saleh said you should place the array outside your view controller. To do it in the
appDelegate, like he was suggesting, do the following:In AppDelegate.h declare:
In AppDelegate.m:
Then in your ViewController.m:
and change your
viewDidLoadmethod:This should work.