UIButton和“下一个”和上一个图像的动作

| 因此,对于iPhone开发人员来说,这是一个相当新的东西,并且可以一直学习。我知道我想要什么,但不知道从哪里开始。在此项目之前,我实际上只处理过tableView应用程序,因此我对imageView和自定义操作的实际知识非常缺乏! 我想让UIImageView加载有图像(使用fetchedResultsController填充的数组,因为图像将存储在CoreData DB中)。 我想要一个带有动作“ next”的UIButton和一个带有动作“ previous”的UIButton,它将控制前面提到的imageViews显示的图像。 到目前为止,我已经完成了数据库映射和接口设计,但是未能找到这些问题的答案(我相信对于那些知道的人来说,这将是很直接的)。 任何建议或示例代码将不胜感激。 脱酒石酸 下面的@shawnwall代码段...
-(IBAction)nextImage { 
    index++; 
    int imageCount=31; 
    if (index<=imageCount) { 
        NSString* imageName=[NSString stringWithFormat:@\"img%i.jpg\", index];
        [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 
植入viewDidLoad中的可变数组:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@\"Images\"      inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@\"topImage\" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResultsT = [[managedObjectContext executeFetchRequest:request error:&error]mutableCopy];
if (mutableFetchResultsT == nil) {
}

[self setTopImageArray:mutableFetchResultsT];

NSLog(@\"%i\" , [topImageArray count]);
正在将图像添加到数组中:
Images *imageS = (Images *)[NSEntityDescription insertNewObjectForEntityForName:@\"Images\" inManagedObjectContext:managedObjectContext];

imageS.topImage = selectedImage;


[topImageArray insertObject:imageS.topImage atIndex:0];

NSLog(@\"%i\" , [topImageArray count]);
... ... 同样的方法,但是在创建缩略图之后,我打电话给:
[self updateTopIV];
这是:
-(void)updateTopIV 
{
topIV.image = [topImageArray objectAtIndex:0];
}
然后,我有2个动作按钮(下一个/上一个):
- (IBAction)nextTouch
{
[self showPhoto:1]; 
}

- (IBAction)previousTouch
{
[self showPhoto:-1];
}


 - (void)showPhoto:(NSInteger)numberToAdd
{
    topIV.image = [topImageArray objectAtIndex:currentPhotoIndex + numberToAdd];

}
    
已邀请:
        将图像放在一个数组中,并将当前图像的索引放在一个变量中
NSInteger *currentPhotoIndex;

- (IBAction)nextTouch
{
    [self showPhoto:1]; // number to add to current, so 1 for next, -1 for previous
}

- (IBAction)previousTouch
{
    [self showPhoto:-1];
}


// Actual \"Logic\"

- (void)showPhoto:(NSInteger)numberToAdd
{
    NSArray *arrayOfImages = allTheImagesYouWantToShow
    UIImageView.image = [arrayOfImages objectAtIndex:currentPhotoIndex + numberToAdd];
    // Adding one or subtracting one from the current image\'s index will show the photo
    immediately before or after it. With this method, you can also skip around to any
    number you want.
}
我认为应该这样做。 - 编辑: 完成后,应在头文件中定义currentPhotoIndex。应该将其设置为您显示的第一个图像的数组中的位置。现在,让我们假设它是第一个。 第一次显示图像时,请执行以下操作:
currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI\'mShowingRightNow];
另外,好抓。为了防止NSInteger超出阵列范围,您需要执行以下操作:
- (void)showPhoto:(NSInteger)numberToAdd
{
    if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
    {
        // Show new image
    }
}
    

bab

        在视图中的.xib中放置两个UIButton \(如果使用的是IB)。 在视图控制器.h中创建两个IBAction \:
-(IBAction)nextClicked:(id)sender;
-(IBAction)prevClicked:(id)sender;
和.m:
-(IBAction)nextClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

-(IBAction)prevClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}
接下来,通过IB将这些动作链接到按钮的“内部触摸”事件。这将使这些事件触发。     

要回复问题请先登录注册