0

私は を使用しており、内部でボタンをインスタンス化した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!");
}
4

1 に答える 1

0

次のコード行を置き換えます。

@property (nonatomic, assign) id delegate;

これとともに:

@property (nonatomic, weak) id<CustomCellDelegate> delegate;

割り当てを使用すると強いオブジェクトがあり、UICollectionViewセルを解放できなかったため、弱いようなプロパティも設定しました

于 2013-10-11T14:35:30.927 に答える