0

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

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

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

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

@interface PhotosTableViewController : UITableViewController <CameraSessionViewDelegate>

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

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

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

Thanks!!

Kurr0
  • 197
  • 1
  • 10
  • You have not implemented in PhotosTableViewContoller the -(void)uploadPhotosFromCamera:(NSMutableArray*)photos; I think that is pretty much what was is missing. – dirtydanee Oct 19 '15 at 09:38

3 Answers3

3

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;

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

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.

Tim
  • 8,932
  • 4
  • 43
  • 64
  • -uploadPhotosFromCamera method was already implemented, I forget to paste it. This IF (...respondsToSelector... ) is never done... self.delegado is nil – Kurr0 Oct 19 '15 at 10:31
  • At what stage do you add the CameraSessionView to the screen? – Tim Oct 19 '15 at 11:19
  • I have a tableview, and in when I press on the first cell I cant select the way to add photos to the tableview, from the gallery or from the camera (CameraSessionView) this may help you https://www.dropbox.com/s/yp7lblacj2ujohw/2015-10-19%2013.30.06.png?dl=0 – Kurr0 Oct 19 '15 at 11:31
  • Yes but where in code, can you post that? Seems your delegate is being deallocated after you set it before you can call back. – Tim Oct 19 '15 at 12:37
  • When I push the first cell, I call an actionsheet and depends of selections perform a segue to camerasessionview... – Kurr0 Oct 19 '15 at 12:45
0

This is working for me. So, you can implement this Directly. Hope, you will get success. Oh! first check without camera activity. Just pass simple array of string to test the delegate

/............*****************

CameraSessionView.h file

/............*****************

#import <UIKit/UIKit.h>

@class CameraSessionView;

@protocol CameraSessionViewDelegate <NSObject>

@optional
-(void)uploadPhotosFromCamera:(NSMutableArray*)photos;

@end

@interface CameraSessionView : UIViewController

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

@end

/............*****************

CameraSessionView.m file

/............*****************

#import "CameraSessionView.h"

@interface CameraSessionView ()
@property (nonatomic, strong) NSMutableArray* images;
@end

@implementation CameraSessionView


- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(onTapOkButton)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"OK" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}

- (void)onTapOkButton{

NSLog(@"Save photos");
_images = [[NSMutableArray alloc]init];
[_images addObject:@"_images1"];
[_images addObject:@"_images2"];

//NSLog(@"from del:%@",_images);

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

[self onTapDismissButton];
}
-(void)onTapDismissButton{
[self.view removeFromSuperview];
}

@end

/............*****************

DetailViewController.m file

/.........********

#import "DetailViewController.h"
#import "CameraSessionView.h"


@interface DetailViewController ()<CameraSessionViewDelegate>

@end


@implementation DetailViewController

- (void)viewDidLoad {
[super viewDidLoad];
CameraSessionView *Camara= [[CameraSessionView alloc]initWithNibName:@"CameraSessionView" bundle:nil];
[Camara setDelegado:self];
[self.navigationController pushViewController:Camara animated:YES];
}


-(void)uploadPhotosFromCamera:(NSMutableArray*)photos{
NSLog(@"success:%@",photos);
}
@end
Jamil
  • 2,977
  • 1
  • 13
  • 23
0

If you have to pass data from B View Controller To A view Controller

  1. Create protocol in B View Controller as

    @protocol BViewControllerDelegate <NSObject>
    -(void)didclickonSubmit:(NSArray*)selected_array;
    @end
    
  2. Create an id, so that you can assign any class as its delegate class.

    @property (weak,nonatomic) id<BViewControllerDelegate> delegate;
    
  3. Call this method in B View Controller on submit button or wherever required.

    if (self.delegate && [self.delegate respondsToSelector:@selector(didclickonSubmit:)])
    {
    [self.delegate didclickonSubmit:myarray];
    }
    
  4. Create an object of B View Controller in View Controller A and assign A as delegate of B

    BViewController *b = [[BViewController alloc]init];
    b.delegate=self;
    
  5. Implement required protocol methods of B in A and access the array

      -(void)didclickonSubmit:(NSArray*)array
      {              
         NSArray *myarray =[[NSMutableArray alloc]initWithArray:array];
      }
    

    now you can use myarray,as u like it..

hit link for sample project https://www.dropbox.com/s/002om8efpy6fout/passDataToPreviousContoller.zip Hope it helps..

****EDITED**** u can for sure assign tableViewController as delegate of UIView class.

@protocol BView <NSObject>
-(void) didclickonSubmit:(NSArray*) selected_array;
@end
@interface BView : UIView
@property (weak,nonatomic) id<BView> delegate;
@end  

  in A i.e. your tableViewController create an object of B and assign your tableview controller as delegate of B .


BView *b=[[BView alloc]init];                                                                                            
b.delegate=self;

Happy Coding..:)

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
  • I think there is an issue... in my proyect... ViewControllerA is TableViewController and ViewControllerB is an UIView... would be any problem? – Kurr0 Oct 19 '15 at 11:46
  • It shouldn't be the problem. u can for sure assign tableViewController as delegate of UIView class. I have updated my ans. – luckyShubhra Oct 19 '15 at 12:25
  • Your code wrks for me , have u tried with debugger.. put a debugger on where you have implemented delegate method of B in tableViewController. Does the debugger reaches there after clicking submit button..??? If it does then problem must be in the path where you have saved the images. – luckyShubhra Oct 20 '15 at 04:33