在 ObjectiveC 上的代表之间传回信息 [英] Passing back info between delegates on ObjectiveC

查看:29
本文介绍了在 ObjectiveC 上的代表之间传回信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 CameraSessionView 之间传回 NSMutableArray 照片;如何将相机拍摄的照片存储在 NSMutableArray 上,以及 TableViewController 如何将这些照片上传到 DropBox.我正在使用委托和协议,但我尝试的所有方式......都失败了.任何人都可以帮助我.我认为我做错了一些小事.我给你看一些代码:

I need to pass back an NSMutableArray of photos between a CameraSessionView; how store the photos taken from camera on an NSMutableArray, and a TableViewController how uploads this photos to DropBox. I'm using delegates and protocols, but all the ways I tried... fail. Anyone can help me. I think Im doing some little thing wrong. I show you some code:

CameraSessionView.h

CameraSessionView.h

@class CameraSessionView;
@protocol CameraSessionViewDelegate <NSObject>
@optional
-(void)uploadPhotosFromCamera:(NSMutableArray*)photos;
@end

@property (nonatomic, weak) id <CameraSessionViewDelegate> delegado;

CameraSessionView.m

CameraSessionView.m

@property (nonatomic, strong) NSMutableArray* images;

- (void)onTapOkButton{

    NSLog(@"Save photos");

    if([self.delegado respondsToSelector:@selector(uploadPhotosFromCamera:)])
    [self.delegado uploadPhotosFromCamera:_images];

    [self onTapDismissButton];
}

PhotosTableViewController.h

PhotosTableViewController.h

@interface PhotosTableViewController : UITableViewController <CameraSessionViewDelegate>

PhotosTableViewController.m

PhotosTableViewController.m

@property (nonatomic, strong) CameraSessionView *camera;
- (void)viewDidLoad
{
_camera = [CameraSessionView new];
[_camera setDelegado:self];
}
-(void)uploadPhotosFromCamera:(NSMutableArray*)photos
{
    NSLog(@"UPFC");
    for(int x=0; x < [photos count];x++)
    {
        NSLog(@"UPFC...");

        UIImage *foto = [photos objectAtIndex:x];

         if (foto.size.height > 1000 || foto.size.width > 1000)
             foto = [self imageWithImage:foto scaledToScale:0.15f];

         DBMetadata* datos = [TablaSubidas addFile:pathElemento];
         NSFileManager *fileManager = [NSFileManager defaultManager];
         NSData *data = UIImageJPEGRepresentation(foto, 1.0);
         [fileManager createFileAtPath:[self photoPath:datos] contents:data attributes:nil];
         [elementosTabla insertObject:datos atIndex:0];
    }

    [self sincFotos];
    [self.tableView reloadData];
}

只希望当我按下 OK 按钮时,照片会发送回 PhotosTableViewController 并上传到 dropbox.

Only wants that when I press OK button the photos send back to PhotosTableViewController where it would be uploaded to dropbox.

onTapOKButton 上的 self.delegado 始终为 nil.

self.delegado on onTapOKButton is always nil.

看起来很简单,但我无法运行它.如果有人可以帮助我或向我推荐任何教程,我将不胜感激......

Looks easy but I cant run it. I'm so grateful if anyone could help me or recommend me any tutorial...

谢谢!!

推荐答案

您的 CameraSessionView 实例将在 viewDidLoad 结束后立即从内存中释放.您需要将其存储在 PhotosTableViewController 的属性中,以便保留.

Your CameraSessionView instance will be released from memory as soon as viewDidLoad ends. You need to store it in a property in PhotosTableViewController so that it is retained.

您的委托也应该被定义为弱,例如

Your delegate should also be defined as weak, e.g.

@property (nonatomic,weak) id< CameraSessionViewDelegate >delegado;

然后在您的 PhotosTableViewController 实现中,您需要实现 -(void)uploadPhotosFromCamera:(NSMutableArray*)photos; 方法.

Then in your implementation of PhotosTableViewController, you'll need to implement the -(void)uploadPhotosFromCamera:(NSMutableArray*)photos; method.

由于这个方法被定义为@optional,你应该在调用它之前检查委托是否响应它.

Also as this method is defined as @optional, you should check if the delegate responds to it before calling it.

   if([self.delegado respondsToSelector:@selector(uploadPhotosFromCamera:]){
        [self.delegado uploadPhotosFromCamera:_images];
   }

如果未实现委托方法,这将防止应用崩溃.

This will prevent the app from crashing if the delegate method isn't implemented.

这篇关于在 ObjectiveC 上的代表之间传回信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆