2015-10-19 69 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