2013-10-11 14 views
0

我正在使用UICollectionView和我爲我的「單元」類CustomCell創建的,其中我實例化了一個按鈕。如果我將動作放在「CustomCell.m」中,回覆日誌並給我寫東西,但我需要使用包含UICollectionView的類中的動作按鈕。我無法使CustomCell中的按鈕動作

因爲這個原因,我使用它的代表,但我不工作。

我該如何解決?

這是我加入到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

替換這行代碼:

@property (nonatomic, assign) id delegate; 

與此:

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

我還設置像弱財產,因爲與指定你有一個強大的對象,並在UICollectionView那麼你的單元不能是r被選中

+0

不應該是相同的...... –

+0

當您使單元格廣告結束之前返回您設置單元格委託的單元格cell.delegate = self; –

+0

@GianfrancoCotumaccio是的是一樣的,但是這樣做是好事:) –