CollectionViewController

CollectionViewController

按下按钮时,我试图将数据从Collection View Controller发送到View Controller。这是我到目前为止的内容:

CollectionViewController.h

#import <UIKit/UIKit.h>

@class CollectionViewController;

@protocol CollectionViewDelegate <NSObject>

-(void) sendTest;

@end
@interface CollectionViewController : UICollectionViewController

@property (nonatomic, weak) id<CollectionViewDelegate> deligate;

@end


CollectionViewController.m

-(void) viewDidLoad {
  [super viewDidLoad];
}

...

NSLog(@"check");
[self.deligate sendTest];
NSLog(@"called");


FooViewController.m

#import "CollectionViewController.h"

@interface FooViewController () <CollectionViewDelegate> {}
@end

@implementation FooViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CollectionViewController *controler = [[CollectionViewController alloc] init];
    controler.deligate = self;
}

- (void) sendTest {
    NSLog(@"Delegates are great!");
}


不幸的是,当我呼叫[self.deligate sendTest];时,什么都没有发生。我知道它被称为,因为我得到了checkcalled日志。

最佳答案

主要的错误是您自己创建了一个CollectionViewController,但您不应该这样做。所需的CollectionViewController由情节提要板自动实例化。如何找到这个视图控制器?

1-在FooViewController的搜索期间捕获它:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    CollectionViewController *collectionViewController =(CollectionViewController *) segue.destinationViewController;
    collectionViewController.deligate = self;
}


2-在FooViewController的子视图控制器中找到它

self.childViewControllers


有帮助吗?

关于ios - 使用委托(delegate)将数据从Collection View Controller传递到View Controller,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47859747/

10-09 09:38