在ObjectiveC上的委托之间传回信息
我需要在CameraSessionView之间传回一张NSMutableArray的照片; 如何将从摄像头拍摄的照片存储在NSMutableArray上,以及如何将这些照片上传到DropBox的TableViewController。 我正在使用委托和协议,但我尝试的所有方式…失败。 任何人都可以帮助我。 我觉得我做错了一些小事情。 我给你看一些代码:
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]; }
只有当我按OKbutton时,照片才会发回到PhotosTableViewController的上传到Dropbox的位置。
onTapOKButton上的self.delegado始终为零。
看起来很容易,但我不能运行它。 我很感激,如果有人可以帮我或推荐我任何教程…
谢谢!!
一旦viewDidLoad结束, CameraSessionView
实例将从内存中释放。 您需要将其存储在PhotosTableViewController
中的一个属性中,以便保留它。
你的代表也应该被定义为弱,例如
@property (nonatomic,weak) id< CameraSessionViewDelegate >delegado;
然后在你的PhotosTableViewController的实现中,你需要实现-(void)uploadPhotosFromCamera:(NSMutableArray*)photos;
方法。
另外,由于这个方法被定义为@optional
,你应该在调用之前检查委托是否响应。
if([self.delegado respondsToSelector:@selector(uploadPhotosFromCamera:]){ [self.delegado uploadPhotosFromCamera:_images]; }
这将防止应用程序崩溃,如果委托方法没有实现。
这是为我工作。 所以,你可以直接实现这个。 希望,你会获得成功。 哦! 首先检查没有相机的活动。 只需传递简单的string 数组来testing委托
/…………*****************
CameraSessionView.h文件
/…………*****************
#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文件
/…………*****************
#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文件
/………********
#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
如果必须将数据从B视图控制器传递到视图控制器
-
在B视图控制器中创build协议为
@protocol BViewControllerDelegate <NSObject> -(void)didclickonSubmit:(NSArray*)selected_array; @end
-
创build一个ID,以便您可以分配任何类作为其委托类。
@property (weak,nonatomic) id<BViewControllerDelegate> delegate;
-
在B视图控制器提交button或任何需要的地方调用此方法。
if (self.delegate && [self.delegate respondsToSelector:@selector(didclickonSubmit:)]) { [self.delegate didclickonSubmit:myarray]; }
-
在View Controller A中创build一个B View Controller的对象,并将A分配为B的代理
BViewController *b = [[BViewController alloc]init]; b.delegate=self;
-
在A中实现B所需的协议方法并访问该数组
-(void)didclickonSubmit:(NSArray*)array { NSArray *myarray =[[NSMutableArray alloc]initWithArray:array]; }
现在你可以使用myarray,就像你喜欢它..
点击示例项目的链接https://www.dropbox.com/s/002om8efpy6fout/passDataToPreviousContoller.zip希望它有助于..
**** EDITED ****你可以肯定分配tableViewController作为委托的UIView类。
@protocol BView <NSObject> -(void) didclickonSubmit:(NSArray*) selected_array; @end @interface BView : UIView @property (weak,nonatomic) id<BView> delegate; @end in A ie your tableViewController create an object of B and assign your tableview controller as delegate of B . BView *b=[[BView alloc]init]; b.delegate=self;
快乐编码.. 🙂