私は を使用しており、内部でボタンをインスタンス化したUICollectionView
「セル」クラス用に作成しました。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!");
}