2015-10-19 68 views
0

我需要在CameraSessionView之间传回一张NSMutableArray的照片;如何将从摄像头拍摄的照片存储在NSMutableArray中,以及如何将这些照片上传到DropBox中的TableViewController。我使用委托和协议,但我尝试过的所有方式都失败了。 任何人都可以帮助我。我认为我做错了一些小事情。 我告诉你一些代码:在ObjectiveC上的代理之间传回信息

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

只希望当我按下OK按钮时,照片将发送回PhotosTableViewController,并将其上传到Dropbox。

onTapOKButton上的self.delegado始终为零。

看起来很容易,但我无法运行它。 我很感激,如果任何人都可以帮助我或推荐我任何教程...

谢谢!

+0

您还没有PhotosTableViewContoller实施 - (空)uploadPhotosFromCamera:(NSMutableArray的*)照片; 我觉得这几乎是缺少的东西。 – dirtydanee

回答

3

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

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

+0

-uploadPhotosFromCamera方法已经实施,我忘了粘贴它。 这个IF(... respondsToSelector ...)从来没有完成... self.delegado是零 – KurroCantos

+0

你在什么阶段添加CameraSessionView到屏幕上? – Tim

+0

我有一个tableview,并且当我按下第一个单元格时,我无法选择将照片添加到桌面视图的方式,从画廊或相机(CameraSessionView),这可能会帮助您https://www.dropbox.com /s/yp7lblacj2ujohw/2015-10-19%2013.30.06.png?dl=0 – KurroCantos

0

这对我有用。所以,你可以直接实现这个。希望,你会获得成功。哦!首先检查没有相机活动。只是通过简单阵列的测试委托

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

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 
+0

我做了改变,杰夫告诉我,看起来更好...但仍然没有调用代理方法 – KurroCantos

+0

你有没有实现“ - (void)uploadPhotosFromCamera:(NSMutableArray *)照片{ NSLog(@”success:%@“,照片); }“ – Jamil

+0

方法在PhotosTableViewController.m文件? – Jamil

0

如果必须从乙视图控制器的数据传送到一个视图控制器

  1. 在乙视图控制器作为

    @protocol BViewControllerDelegate <NSObject> 
    -(void)didclickonSubmit:(NSArray*)selected_array; 
    @end 
    
  2. 创建协议创建的ID,这样就可以分配任何类作为其代表班。

    @property (weak,nonatomic) id<BViewControllerDelegate> delegate; 
    
  3. 在B视图控制器上提交按钮或任何需要的地方调用此方法。

    if (self.delegate && [self.delegate respondsToSelector:@selector(didclickonSubmit:)]) 
    { 
    [self.delegate didclickonSubmit:myarray]; 
    } 
    
  4. 创建视图控制器A B视图控制器的一个对象,并指定一个为B的代表

    BViewController *b = [[BViewController alloc]init]; 
    b.delegate=self; 
    
  5. 在一个工具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 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; 

编码快乐.. :)

+0

我认为有一个问题...在我的proyect ... ViewControllerA是TableViewController和ViewControllerB是一个UIView ...会有什么问题吗? – KurroCantos

+0

它不应该是问题。你可以确定分配tableViewController作为UIView类的委托。我已经更新了我的答案。 – luckyShubhra

+0

你的代码wrks对我来说,你用调试器试过..把调试器放在你已经在tableViewController中实现了B的委托方法的地方。点击提交按钮后,调试器是否到达那里?如果确实如此,则问题必须出现在保存图像的路径中。 – luckyShubhra