即使计算并返回了正确数量的对象,NSArray 也返回 NULL 值 [英] NSArray returning NULL value even though right number of objects is counted and returned

查看:56
本文介绍了即使计算并返回了正确数量的对象,NSArray 也返回 NULL 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含图像对象的 NSArray.根据 UISwipeGestureRecognizerDirection 识别的滑动​​方向,索引向上或向下递增,以便选择正确的图像.

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.

我遇到的问题是我的 NSArray 返回一个 NULL 值而不是图像对象.因此 UIImageView(名为 imageView)什么也不显示.

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.

我正在打印一个 NSLog 语句以查看实际发生的情况 - 下面的示例:

I am printing an NSLog statement in order to see what is really happening - example below:

... [2307:60b] 滑动,索引为:1,图像为(空),物体总数为 2

... [2307:60b] Swiped and the index is: 1, the image is (null) and the total number of objects 2

根据上面的 NSLog 语句,我们可以看到:- 对象的数量被完美计算.- NSArray 索引完全向上和向下递增.- 但是为图像对象返回了一个 NULL 值.

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.

我的代码如下(见handleSwipe方法):

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

推荐答案

在您的 .h 文件中(如果 UIImageView not 在界面构建器中),将属性声明更改为:

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

@property (nonatomic, strong) UIImageView *imageView;

你可以把strong去掉,保留为(nonatomic),但如果你不熟悉weak vs. strong,它是很好的参考.如果您将其保留为弱指针,则该对象将在此视图控制器中赋值后立即释放,因为不会有其他强指针指向它.

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.

如果 UIImageView 没有在界面构建器中设置,那么你需要在实现中创建它:

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.

这篇关于即使计算并返回了正确数量的对象,NSArray 也返回 NULL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆