如何在 Objective-C 中获取 NSArray 中的当前位置

发布于 2024-12-29 00:39:59 字数 539 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚然 2025-01-05 00:39:59

数组没有当前位置。它们不像流,它们只是容器。

因此,有两种方法可以解决此问题:

  1. 将数组位置单独保留为视图控制器实例中的 NSInteger。根据需要增加和减少它。使用 objectAtIndex 获取那里的对象。
  2. 使用 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:

  1. Keep your array position separately, as an NSInteger in your view controller instance. Increment and decrement it as needed. Use objectAtIndex to get the object there.
  2. Find the position of your current image using indexOfObject. Increment and decrement that as needed. Use objectAtIndex to get the object there.

I'd recommend the first approach.

不美如何 2025-01-05 00:39:59
- (IBAction)swipeRightGesture:(id)sender 
{
     NSUInteger index = [imageArray indexOfObject:imageView.image] + 1;

     // do something here to make sure it's not out of bounds
     //

     imageView.image = [imageArray objectAtIndex:index];//load next object in array
}
- (IBAction)swipeRightGesture:(id)sender 
{
     NSUInteger index = [imageArray indexOfObject:imageView.image] + 1;

     // do something here to make sure it's not out of bounds
     //

     imageView.image = [imageArray objectAtIndex:index];//load next object in array
}
花开半夏魅人心 2025-01-05 00:39:59

下面给出解决方案。

-(void)next
{
    if (currentIndex < (count - 1))
    {
        currentIndex ++;
        NSLog(@"current index : %d", currentIndex);
    }
}

-(void) previous
{
    if (currentIndex > 1) {
        currentIndex --;
        [self.commsLayer  performOperation:currentIndex];
        NSLog(@"current index : %d", currentIndex);
    }else
    {
        if (currentIndex == 1) {
            currentIndex --;
            NSLog(@"first current index : %d", currentIndex);
        }
    }
}

The solution is given below .

-(void)next
{
    if (currentIndex < (count - 1))
    {
        currentIndex ++;
        NSLog(@"current index : %d", currentIndex);
    }
}

-(void) previous
{
    if (currentIndex > 1) {
        currentIndex --;
        [self.commsLayer  performOperation:currentIndex];
        NSLog(@"current index : %d", currentIndex);
    }else
    {
        if (currentIndex == 1) {
            currentIndex --;
            NSLog(@"first current index : %d", currentIndex);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文