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!!
Your
CameraSessionView
instance will be released from memory as soon as viewDidLoad ends. You need to store it in a property inPhotosTableViewController
so that it is retained.Your delegate should also be defined as weak, e.g.
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.This will prevent the app from crashing if the delegate method isn't implemented.
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
/............*****************
/............*****************
CameraSessionView.m file
/............*****************
/............*****************
DetailViewController.m file
/.........********
If you have to pass data from B View Controller To A view Controller
Create protocol in B View Controller as
Create an id, so that you can assign any class as its delegate class.
Call this method in B View Controller on submit button or wherever required.
Create an object of B View Controller in View Controller A and assign A as delegate of B
Implement required protocol methods of B in A and access the 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.
Happy Coding..:)