我正在使用UICollectionView
并为我的“cell”类CustomCell
创建,其中我实例化了一个按钮。如果我将操作放在“CustomCell.m
”中,则会响应日志并写入一些内容,但我需要使用包含UICollectionView
的类中的操作按钮。
我出于这个原因使用了代表,但我仍然无法工作。
我该如何解决?
这是我添加到CustomCell.h的代码:
@class CustomCell;
@protocol CustomCellDelegate
- (void)btnSaveColor:(CustomCell *)sender;
@end
@interface CustomCell : UICollectionViewCell
@property (nonatomic, assign) id delegate;
@property (strong, nonatomic) IBOutlet UILabel *lblCustomCell;
@property (strong, nonatomic) IBOutlet UIView *bgCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *RCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *GCustomCell;
@property (strong, nonatomic) IBOutlet UILabel *BCustomCell;
- (IBAction)btnSaveColor:(id)sender;
@end
这是我添加到CustomCell.m
的代码#import "CustomCell.h"
@implementation CustomCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (IBAction)btnSaveColor:(CustomCell *)sender {
[_delegate btnSaveColor:self];
}
@end
这是在ViewController.h中输入的代码
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "CustomCell.h"
@interface gradientViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, CustomCellDelegate>
@property (nonatomic, strong) NSString *hexCode;
@property (strong, nonatomic) IBOutlet UILabel *lblDest;
@property (strong, nonatomic) IBOutlet UICollectionView *gradientCollectionView;
@property (strong, nonatomic) NSArray *gradientArray;
@property (strong, nonatomic) NSArray *gradientArrayToWhite;
@property (strong, nonatomic) NSArray *allGradient;
@property (strong, nonatomic) NSArray *reverse;
@property (strong, nonatomic) NSArray *sorted;
@property (strong, nonatomic) IBOutlet UIView *bgHEXColorGradient;
- (IBAction)btnBack:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *bgBack;
@property (nonatomic, strong) NSArray *ColorSchemeAnalagous;
@property (nonatomic, strong) NSArray *ColorSchemeComplementary;
@property (nonatomic, strong) NSArray *ColorSchemeTriad;
@end
和ViewController.m中的这个
#import "gradientViewController.h"
#import "ViewController.h"
#import "UIColor+Expanded.h"
#import "CustomCell.h"
#import <QuartzCore/QuartzCore.h>
#import "ColorUtils.h"
#import "MPColorTools.h"
#import "RecipeCollectionHeaderView.h"
#import "UIColor+Colours.h"
@interface gradientViewController ()
@end
@implementation gradientViewController
- (void)btnSaveColor:(CustomCell *)sender {
NSLog(@"Funziona il delegato!");
}
答案 0 :(得分:0)
替换这行代码:
@property (nonatomic, assign) id delegate;
用这个:
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
我也设置了弱像属性,因为在分配时你有一个强大的对象而在UICollectionView
中你的Cell无法释放