我有一个CollectionViewController和CollectionViewCell。我正在从数据库中获取数据,因此当加载控制器时,它会动态创建相应的单元格。
每个单元格都有一个UIBUTTON和UITEXTVIEW。我正在使用uibutton显示图片(如果存在于数据库中)或捕获图像(如果按下)。
InboundCollectionViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
InboundCollectionViewCell *inboundDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InboundDetailCell" forIndexPath:indexPath];
Image *current = [images objectAtIndex:indexPath.row];
[inboundDetailCell.imageType setText:[NSString stringWithFormat:@"%@", [current pd_description]]];
if ([current.pd_image isKindOfClass:[NSData class]] == NO) {
[inboundDetailCell.imageButton addTarget:self action:@selector(useCamera) forControlEvents:UIControlEventTouchUpInside];
}
else {
[inboundDetailCell.imageButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
return inboundDetailCell;
}
到目前为止,一切都很好。我启动我的应用程序。收集视图控制器可以根据数据库的结果填充单元。
如果图像字段具有图像,则在我的自定义ImageButton的图像属性中加载" check.png"。
如果图像字段没有图像,则将图像键的touchupinside动作设置为" usecamera"。
- (void)useCamera
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[self presentViewController:imagePicker animated:YES completion:NULL];
}
现在,根据我关注的教程,我必须实现以下代码:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// set image property of imageButton equal to the value in UIImage 'image' variable ???
[self dismissViewControllerAnimated:YES completion:NULL];
}
在我发现的大多数示例中,imageView和imagePickerController都是在同一ViewController中创建的。因此,很容易访问ImageView的映像属性(或我的情况下的按钮)。
我的问题是我的" iboutlet uibutton imagebutton"位于InboundCollectionViewCell内部,而不是InboundCollectionViewController。因此,我找不到将返回的图像从相机传递到按钮的图像属性的方法。
请注意,我对目标C和Xcode都是非常陌生的,这是我的第一个项目。:P :)
预先感谢您!
确保UseCamera接收按下的按钮,并将其存储在成员变量中:
:- (void)useCamera:(id)sender {
UIButton *button = (UIButton *)sender;
self.lastButtonPressed = sender; // A member variable
...
}
请注意,由于签名已更改,因此您需要将TouchupIndide重新启动到此功能。
现在,在ImagePickerController中:didfinishpickingmediawithinfo:您可以访问成员变量self.lastbuttonpressed以更新其映像。
tim