【问题标题】:custom delegate is not getting called in IOS自定义委托未在 IOS 中被调用
【发布时间】:2017-05-20 13:49:48
【问题描述】:

大家好,我正在使用 Xcode 8,我正在尝试使用委托在两个视图控制器之间传递对象,但不知何故委托方法没有被调用。我试图纠正但我无法做到。谁能建议我在这里做错了。谢谢。这是我的代码

VideoViewController.h

#import <UIKit/UIKit.h>
#import<AVFoundation/AVFoundation.h>
#import "VideoCollection.h"



//delegate methods
@class VideoViewController;
@protocol AddVideoDelegate <NSObject>

-(void)addVideoToCollectionView:(VideoViewController*)viewController didAddVideo:(VideoCollection*)object;

@end

@interface VideoViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
- (instancetype)initWithVideoUrl:(NSURL *)url;
@property (strong, nonatomic) NSURL *videoUrl;
@property (strong, nonatomic) AVPlayer *avPlayer;
@property (strong, nonatomic) AVPlayerLayer *avPlayerLayer;
@property (strong, nonatomic) UIButton *cancelButton;
@property (strong, nonatomic) UITextField *captionField;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (strong, nonatomic) UIPickerView *pickerView;

@property (nonatomic, weak) id<AddVideoDelegate> delegate;
@end

VideoViewController.m

-(void)addButtonPressed:(UIButton *)button{
    //
    id<AddVideoDelegate> strongDelegate = self.delegate;
    if ([strongDelegate respondsToSelector:@selector(addVideoToCollectionView:didAddVideo:)]) {
        VideoCollection* videoCollection=[[VideoCollection alloc] init];
        videoCollection.videoCaption=self.captionField.text;
        videoCollection.videoURL=_videoUrl;
        videoCollection.videoType=self.videoTypeLabel.text;
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        videoCollection.captionHeightPercentage=(self.captionField.frame.origin.y*100)/screenRect.size.height;

        [strongDelegate addVideoToCollectionView:self didAddVideo:(VideoCollection *)videoCollection];

    }
 NSLog(@"added");
}

videoCollectionViewController.h

#import <UIKit/UIKit.h>
#import "VideoViewController.h"

@interface videoCollectionViewController : UIViewController<AddVideoDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) VideoViewController* _vc;
- (IBAction)click:(id)sender;

- (IBAction)sfds:(id)sender;

@end

videoCollectionViewController.m

#import "videoCollectionViewController.h"
#import "WebSerivceInterface.h"
#import "VideoViewController.h"

@interface videoCollectionViewController ()

@end

@implementation videoCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    __vc=[[VideoViewController alloc] init];
    __vc.delegate=self;





}
-(void)addVideoToCollectionView:(VideoViewController *)viewController didAddVideo:(VideoCollection *)object{
    NSLog(@"delegate called");
    self.label.text=object.videoCaption;
}
@end

【问题讨论】:

  • 嗨,Riya,您能否在 VideoViewController.h 类中创建代理的属性,然后尝试在两个控制器之间传递数据。确保您的委托属性必须是弱的。
  • @MandeepSingh 我部分添加了有问题的 videoViewController.h,但它在文件中
  • 你的VideoViewController是什么,是uiview的子类吗?
  • @MandeepSingh 我已经编辑了问题
  • 这不是在 videoCollectionViewController 类中调用委托的方式。

标签: ios objective-c


【解决方案1】:

你的问题在这里:

   (void)addButtonPressed:(UIButton *)button{
   //
   id<AddVideoDelegate> strongDelegate = self.delegate;
   if ([strongDelegate 
respondsToSelector:@selector(addVideoToCollectionView:didAddVideo:)]) {
       VideoCollection* videoCollection=[[VideoCollection alloc] init];
        videoCollection.videoCaption=self.captionField.text;
       videoCollection.videoURL=_videoUrl;
        videoCollection.videoType=self.videoTypeLabel.text;
     CGRect screenRect = [[UIScreen mainScreen] bounds];
                 videoCollection.captionHeightPercentage= 
  (self.captionField.frame.origin.y*100)/screenRect.size.height;         
 [strongDelegate addVideoToCollectionView:self didAddVideo
      (VideoCollection *)videoCollection];
}
NSLog(@"added");
}    

您正在尝试在此处设置委托自我,这意味着它将返回此类。但是委托也是从调用者类中设置的。委托总是应该从实现方法的那个类中设置。只需删除该行

id strongDelegate = self.delegate; 并在推送/呈现 vc 之前设置委托。 语言不好见谅

【讨论】:

    【解决方案2】:

    假设您在 A 类中创建了自己的自定义委托方法,并且您需要在 B 类中访问该方法。所以当您从 B 类推送或呈现到 A 类时,如下所示:-

    ViewController *objView = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    objView.delegate = self;
    [self.navigationController pushViewController:objView animated:true]; 
    

    ViewController 是您在其中创建委托的 A 类,然后您可以在 B 类中访问您的委托方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 2016-04-16
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多