2017-05-20 7 views
-4

こんにちはすべて私はXcode 8を使用しています.2つのView Controller間でオブジェクトを渡すためにデリゲートを使用しようとしていますが、代理メソッドが呼び出されていません。 it.can誰もが私が間違ってhere.Thank you.Hereやっているものをお勧めしますと、私のコードカスタムデリゲートがIOSで呼び出されていない

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

です

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

こんにちはRiya、VideoViewController.hクラスのデリゲートのプロパティを作成してから、2つのコントローラ間でデータを渡します。デリゲートプロパティが弱くなければならないことを確認してください。 –

+0

@MandeepSingh私は部分的に問題のvideoViewController.hを追加しましたが、ファイルにあります –

+0

あなたのVideoViewControllerは何ですか、それはuiviewのサブクラスですか? –

答えて

-1

あなたはクラスAで独自のカスタムデリゲートメソッドを作成すると仮定し、あなたはクラスBでそのメソッドにアクセスする必要がありますクラスBからクラスAにプッシュまたはプレゼンテーションするときこのように -

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

のViewControllerは、あなたの問題はここにある、あなたがクラスBに

0

をデリゲートメソッドにアクセスできるようにすることができ、あなたのデリゲートを作成している中で、あなたのクラスAである:

(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をプッシュ/プレゼンテーションする前にデリゲートを設定します。 言語が悪いため申し訳ございません。

関連する問題