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

时间:2011-05-30 22:27:44

标签: iphone ios uiimageview uibutton ibaction

所以,对于iPhone开发和学习一直很新。我知道我想要什么,但不知道从哪里开始。我真的只在这个项目之前处理过tableView应用程序,所以我对imageView和自定义操作的实际知识非常缺乏!

我希望UIImageView加载一个图像(使用由fetchedResultsController填充的数组,因为图像将存储在CoreData DB中)。

我想要一个带有“next”动作的UIButton和带有动作“previous”的UIButton,它将控制前面提到的imageViews显示的图像。

到目前为止,我已经进行了数据库映射和界面设计,但未能找到这些要点的答案(我相信对于那些知道的人来说,这将是相当直接的。)

任何建议或示例代码将不胜感激。

DetartrateD

来自下方的@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];

}

2 个答案:

答案 0 :(得分:1)

在视图中将两个UIButton放在.xib中(如果您使用的是IB)。

在视图控制器中创建两个IBAction .h:

-(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将这些操作链接到按钮的“内部触摸”事件。这会使这些事件发生火灾。

答案 1 :(得分:0)

将图像放入数组中,并将当前图像的索引放在变量

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
    }
}