在IOS中没有调用自定义委托

时间:2017-05-20 13:49:49

标签: ios objective-c

大家好我正在使用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

2 个答案:

答案 0 :(得分:0)

你的问题在这里:

   (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之前设置委托。 对不起,语言不好

答案 1 :(得分:-1)

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

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

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