NSArray returning NULL value even though right num

2019-08-31 06:52发布

问题:

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);
}

回答1:

In your .h file (if the UIImageView is not in interface builder), change the property declaration to:

@property (nonatomic, strong) UIImageView *imageView;

You could take out the strong and leave it as (nonatomic), but if you're unfamiliar with weak vs. strong, it's good for reference. If you were to leave it as weak, the object would get released immediately after assignment within this view controller since there would be no other strong pointers to it.

If the UIImageView was not set up in interface builder, then you need to create it in the implementation:

- (void)viewDidLoad
{
    _imageView = [[UIImageView alloc] init];
    ...
}

If this isn't the case, then make sure your capitalization, spelling, etc. is correct when referencing the images and that all images are added to the application target in your project settings.