UIButton 和“下一步”操作和上一张图片
所以,对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将两个 UIButton 放置在 .xib 视图中(如果您使用的是 IB)。
在视图控制器中创建两个 IBAction .h:
和 .m:
接下来,通过 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:
and .m:
Next, link those actions to the 'touch up inside' event of the buttons via IB. This will make these events fire.
将图像放入数组中,并将当前图像的索引放入变量中,
我认为应该这样做。
--
编辑:
currentPhotoIndex 应该像您所做的那样在头文件中定义。它应该设置为您显示的第一张图像的数组中的位置。现在,我们假设这是第一个。
第一次显示图像时,请执行以下操作:
另外,很好的捕捉。为了防止 NSInteger 超出数组范围,您需要这样的东西:
Put the images in an array, and have the current image's index in a variable
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:
Also, good catch. In order to prevent the NSInteger from being out of the array's bounds, you need something like this: