I have created a NSArray that contains images objects. Based on the swipe direction recognized by UISwipeGestureRecognizerDirection the index is incremented up or down so the right image could be selected.
The problem I have is that my NSArray is returning a NULL value instead of the image object. Therefore the UIImageView (named imageView) displays nothing.
I am printing an NSLog statement in order to see what is really happening - example below:
... [2307:60b] Swiped and the index is: 1, the image is (null) and the total number of objects 2
As per NSLog statement above we can see that: - The number of objects is perfectly counted. - NSArray index increments perfectly up and down. - But a NULL value is returned for the image object.
My code below (see the handleSwipe method):
@synthesize imageView;
int imageIndex = 1;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
imageView.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, CGRectGetHeight(self.view.bounds)/2.0f+20);
[self.view addSubview:imageView];
UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self.view addGestureRecognizer:swipeRightGesture];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self.view addGestureRecognizer:swipeLeftGesture];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionLeft;
UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)];
self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil];
}
- (IBAction)handleSwipe:(UIGestureRecognizer *)sender {
int theTotal;
NSArray *images=[[NSArray alloc] initWithObjects: @"image021.jpg", @"image041.jpg", nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex < 0) ? ([images count] -1): imageIndex % [images count];
imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
theTotal = [images count];
NSLog(@"Swiped and the index is: %d, the image is %@ and the total number of objects %d", imageIndex, imageView.image, theTotal);
}