Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 即使计算并返回了正确数量的对象,NSArray仍返回空值_Ios_Objective C_Uiimageview_Nsarray_Uiswipegesturerecognizer - Fatal编程技术网

Ios 即使计算并返回了正确数量的对象,NSArray仍返回空值

Ios 即使计算并返回了正确数量的对象,NSArray仍返回空值,ios,objective-c,uiimageview,nsarray,uiswipegesturerecognizer,Ios,Objective C,Uiimageview,Nsarray,Uiswipegesturerecognizer,我创建了一个包含图像和对象的NSArray。根据UISwipegestureRecognitzerDirection识别的滑动方向,索引向上或向下递增,以便选择正确的图像 我遇到的问题是,我的NSArray返回的是NULL值,而不是image对象。因此UIImageView(名为imageView)不显示任何内容 我正在打印一条NSLog语句,以查看实际发生的情况-示例如下: 。。。[2307:60b]滑动,索引为:1,图像为(空),对象总数为2 根据上述NSLog声明,我们可以看到: -物体的

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

我遇到的问题是,我的NSArray返回的是NULL值,而不是image对象。因此UIImageView(名为imageView)不显示任何内容

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

。。。[2307:60b]滑动,索引为:1,图像为(空),对象总数为2

根据上述NSLog声明,我们可以看到: -物体的数量是精确计算的。 -NSArray索引完全上下递增。 -但是图像对象返回空值

下面是我的代码(请参见HandleSweep方法):

@synthesis-imageView;
int-imageIndex=1;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
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];
UISweepGestureRecognizer*SwiperightSpiration=[[UISweepGestureRecognizer alloc]initWithTarget:自我操作:@selector(HandleSweep:)];
[self.view addGestureRecognitor:SwiperightSpiration];
SwiperightSprotation.direction=UISweepGestureRecognitizerDirectionRight;
UISweepGestureRecognizer*SwipeleftGetTesture=[[UISweepGestureRecognizer alloc]initWithTarget:self action:@selector(HandleSweep:)];
[self.view addgesturecognizer:swipleftgesture];
SwiperightSprotation.direction=UISweepGestureRecognitizerDirectionLeft;
UIBarButtonItem*Discouse_btn=[[UIBarButtonItem alloc]initWithTitle:@“完成”样式:UIBarButtonItemStylePlain目标:自我操作:@selector(dismissModal:)];
self.navigationItem.RightBarButtonims=[NSMutableArray arrayWithObjects:Discouse_btn,nil];
}
-(iAction)无手柄擦除:(UIgestureRecognitor*)发送器{
国际贸易总额;
NSArray*images=[[NSArray alloc]initWithObjects:@“image021.jpg”,“image041.jpg”,nil];
UISweepGestureRecognitzerDirection=[(UISweepGestureRecognitzer*)发送方方向];
开关(方向){
案例UISweepGestureRecognitizerDirectionLeft:
imageIndex++;
打破
案例UISweepGestureRecognitizerDirectionRight:
图像索引--;
打破
违约:
打破
}
imageIndex=(imageIndex<0)?([图像计数]-1):imageIndex%[图像计数];
imageView.image=[UIImage ImageName:[images objectAtIndex:imageIndex]];
总数=[图像计数];
NSLog(@“已刷卡,索引为:%d,图像为%@且对象总数为%d”,imageIndex,imageView.image,总数);
}
在.h文件中(如果UIImageView在interface builder中是而不是),将属性声明更改为:

@property (nonatomic, strong) UIImageView *imageView;
您可以取出
strong
,并将其保留为
(非原子)
,但如果您不熟悉弱与强,则可以作为参考。如果将其保留为弱对象,则在该视图控制器中分配对象后,该对象将立即释放,因为不会有其他指向该对象的强指针

如果未在interface builder中设置UIImageView,则需要在实现中创建它:

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

如果不是这样,请确保引用图像时大小写、拼写等正确,并且所有图像都已添加到项目设置中的应用程序目标中。

imageView在哪里创建/保留?是IBOutlet吗?@John:我在.h文件中做过。如下所示:[at]接口firstStartViewController:UIViewController[at]属性(弱,非原子)IBOutlet UIImageView*imageView;-(iAction)免提擦除:(UIgestureRecognitor*)发送方;因此,如果UIImageView从interface builder连接到您的视图控制器,为什么要将其作为子视图添加到
viewDidLoad
?您实际创建过图像视图吗?您在第一条评论中所说的不是创建图像视图,而是创建一个属性。您仍然需要alloc init one或connect one(如果它是一个插座)(但如果它是一个插座,您不应该将其添加为子视图,那么它是哪一个?)@Martin…或
imageView
本身为零。谢谢,让我试试这个。事实上,我正试图以完全编程的方式来完成它。
- (void)viewDidLoad
{
    _imageView = [[UIImageView alloc] init];
    ...
}