如何在 Objective-C 中获取 NSArray 中的当前位置
iOS 编码新手,所以这可能更多是一个语法问题。我正在尝试实现一个加载图像对象数组的 UIImageView。向右滑动时,我想移动到数组中的下一个对象,并将其加载到视图中。我已经很好地加载了数组,只是不知道在语法上如何获取数组中的当前位置。我意识到这完全是一个n00b问题,所以为你提供免费的声誉信用。
这是一些代码:
- (void)viewDidLoad
{
[super viewDidLoad];
imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"], nil];
imageView.image = [imageArray objectAtIndex:0];
}
- (IBAction)swipeRightGesture:(id)sender {
imageView.image = [imageArray somethingsomething];//load next object in array
}
New to iOS coding, so this is probably more of a syntax question than anything else. I am trying to implement a UIImageView loaded with an array of image objects. On right swipe, I want to move to the next object in the array, and load it into the view. I have the array loaded fine, just don't know how, syntactically, how to get the current position in the array. I realize this is a total n00b question, so free reputation cred for you.
Heres some code:
- (void)viewDidLoad
{
[super viewDidLoad];
imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"], nil];
imageView.image = [imageArray objectAtIndex:0];
}
- (IBAction)swipeRightGesture:(id)sender {
imageView.image = [imageArray somethingsomething];//load next object in array
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数组没有当前位置。它们不像流,它们只是容器。
因此,有两种方法可以解决此问题:
objectAtIndex
获取那里的对象。indexOfObject
查找当前图像的位置。根据需要增加和减少该值。使用objectAtIndex
获取那里的对象。我推荐第一种方法。
Arrays don't have a current position. They're not stream like, they're just containers.
So there's two ways to fix this:
NSInteger
in your view controller instance. Increment and decrement it as needed. UseobjectAtIndex
to get the object there.indexOfObject
. Increment and decrement that as needed. UseobjectAtIndex
to get the object there.I'd recommend the first approach.
下面给出解决方案。
The solution is given below .