UIButton 和“下一步”操作和上一张图片

发布于 2024-11-11 18:52:30 字数 2326 浏览 2 评论 0原文

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

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

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

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

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

DetartrateD

下面的

-(IBAction)nextImage { 
    index++; 
    int imageCount=31; 
    if (index<=imageCount) { 
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 

@shawnwall 代码片段...在 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];

}

So, pretty new to iPhone dev and learning all the time. I know what I want but dont quite know where to start. I have really only dealt with tableView apps before this project so my practical knowledge of imageView's and custom actions is very much lacking!

I want to have a UIImageView loaded with an image (using an array populated by a fetchedResultsController as images will be stored in a CoreData DB ).

I want an UIButton with action "next" and UIButton with action "previous" which will control the previously mentioned imageViews' displayed image.

Thus far I have the DB mapped and interface design but am failing to find this answer to these points (I am sure it will be fairly straight forward to those who know).

Any advise or example code would be greatly appreciated.

DetartrateD

@shawnwall code snippet from below...

-(IBAction)nextImage { 
    index++; 
    int imageCount=31; 
    if (index<=imageCount) { 
        NSString* imageName=[NSString stringWithFormat:@"img%i.jpg", index];
        [picture setImage: [UIImage imageNamed: imageName]]; 
    } 
} 

Mutatable array seeded in 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]);

Image being added to array here:

Images *imageS = (Images *)[NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:managedObjectContext];

imageS.topImage = selectedImage;


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

NSLog(@"%i" , [topImageArray count]);

...
...

Same method but after I have created a thumbnail i call:

[self updateTopIV];

Which is :

-(void)updateTopIV 
{
topIV.image = [topImageArray objectAtIndex:0];
}

I then have the 2 action buttons (next/previous):

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

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


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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我不是你的备胎 2024-11-18 18:52:30

将两个 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 将这些操作链接到按钮的“touch up inside”事件。这将使这些事件火起来。

Place two UIButton's in your view in a .xib (if you are using IB).

Create two IBAction's in your view controller .h:

-(IBAction)nextClicked:(id)sender;
-(IBAction)prevClicked:(id)sender;

and .m:

-(IBAction)nextClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

-(IBAction)prevClicked:(id)sender
{
  //do stuff
  imageView.image = whatever;
}

Next, link those actions to the 'touch up inside' event of the buttons via IB. This will make these events fire.

找个人就嫁了吧 2024-11-18 18:52:30

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

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

Put the images in an array, and have the current image's index in a variable

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

I think that should do it.

--
EDIT:

currentPhotoIndex should be defined in your header file as you have done. It should be set to the position in the array of the first image you show. For now, lets assume it's the first one.

The first time that you show an image, do this:

currentPhotoIndex = [arrayOfImages indexOfObject:thePhotoI'mShowingRightNow];

Also, good catch. In order to prevent the NSInteger from being out of the array's bounds, you need something like this:

- (void)showPhoto:(NSInteger)numberToAdd
{
    if (currentPhotoIndex > 0 && < arrayOfImages.count) // Makes sure that the int is within the array
    {
        // Show new image
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文