【问题标题】:Passing back info between delegates on ObjectiveC在 Objective C 中的委托之间传回信息
【发布时间】:2016-01-17 14:28:05
【问题描述】:

我需要在 CameraSessionView 之间传回 NSMutableArray 照片;如何将相机拍摄的照片存储在 NSMutableArray 上,以及 TableViewController 如何将这些照片上传到 DropBox。我正在使用委托和协议,但我尝试的所有方式......都失败了。 任何人都可以帮助我。我认为我做错了一些小事。 我给你看一些代码:

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 始终为零。

看起来很简单,但我无法运行它。 如果有人可以帮助我或向我推荐任何教程,我将不胜感激......

谢谢!!

【问题讨论】:

  • 你没有在 PhotosTableViewContoller 中实现 -(void)uploadPhotosFromCamera:(NSMutableArray*)photos;我认为这几乎就是缺少的东西。

标签: ios objective-c delegates tableview


【解决方案1】:

您的CameraSessionView 实例将在 viewDidLoad 结束后立即从内存中释放。您需要将其存储在PhotosTableViewController 的属性中以便保留。

您的委托也应该被定义为弱,例如

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

然后在您的 PhotosTableViewController 实现中,您需要实现 -(void)uploadPhotosFromCamera:(NSMutableArray*)photos; 方法。

由于这个方法被定义为@optional,你应该在调用它之前检查委托是否响应它。

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

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

【讨论】:

  • -uploadPhotosFromCamera 方法已经实现了,我忘记粘贴了。这个 IF (...respondsToSelector... ) 永远不会完成... self.delegado 是 nil
  • 在什么阶段将CameraSessionView添加到屏幕上?
  • 我有一个表格视图,当我按下第一个单元格时,我无法选择从图库或相机 (CameraSessionView) 将照片添加到表格视图的方式,这可能会对您有所帮助 @987654321 @
  • 是的,但是代码在哪里,你能把它贴出来吗?似乎您的委托在您设置后被解除分配,然后您才能回拨。
  • 当我按下第一个单元格时,我会调用一个操作表并根据选择执行对 camerasessionview 的 segue...
【解决方案2】:

这对我有用。所以,你可以直接实现这个。希望,你会获得成功。哦!首先检查没有相机活动。只需传递 string 的简单 array 来测试 delegate

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

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

【讨论】:

  • 我做了 Jeff 告诉我的更改,看起来更好......但仍然没有调用 delegado 方法
  • 你实现了吗"-(void)uploadPhotosFromCamera:(NSMutableArray*)photos{ NSLog(@"success:%@",photos); }"
  • PhotosTableViewController.m 文件中的方法?
【解决方案3】:

如果你必须将数据从 B 视图控制器传递到 A 视图控制器

  1. 在 B 视图控制器中创建协议为

    @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. 在 View Controller A 中创建 B View Controller 的对象,并将 A 指定为 B 的 delegate

    BViewController *b = [[BViewController alloc]init];
    b.delegate=self;
    
  5. 在A中实现B所需的协议方法并访问数组

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

    现在您可以根据自己的喜好使用 myarray..

点击示例项目的链接 https://www.dropbox.com/s/002om8efpy6fout/passDataToPreviousContoller.zip 希望对你有帮助。。

****已编辑**** 你可以确定将 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;

快乐编码..:)

【讨论】:

  • 我认为有一个问题...在我的项目中... ViewControllerA 是 TableViewController 而 ViewControllerB 是 UIView...会有什么问题吗?
  • 应该不是问题。你可以确定将 tableViewController 指定为 UIView 类的代表。我已经更新了我的答案。
  • 您的代码对我有用,您是否尝试过使用调试器.. 将调试器放在您在 tableViewController 中实现 B 的委托方法的位置。单击提交按钮后调试器是否到达那里..???如果确实如此,那么问题一定出在您保存图像的路径中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
相关资源
最近更新 更多