即使计算并返回了正确数量的对象,NSArray 也返回 NULL 值

编程入门 行业动态 更新时间:2024-10-25 14:26:24
本文介绍了即使计算并返回了正确数量的对象,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 值

本文发布于:2023-11-08 11:38:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   对象   正确   NULL   NSArray

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!