2012-11-12 36 views
0

我現在在GalleryScrollView.m中只有1個錯誤。我想我宣佈畫廊按鈕錯了。錯誤實例方法-initwithFrame:imageName:找不到imgname Objective-C

的錯誤是:

Instance Method -initwithFrame:imageName:imgname not found 

這一行:

GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName]; 

我已經試過- (void) removeAttachment:(GalleryButton *)button imageName;

,但似乎並沒有工作。以下是我的代碼。

任何意見或回答非常感謝。

謝謝。

GalleryScrollView.h

#import <UIKit/UIKit.h> 
#import "AttachmentItem.h" 
#import "GalleryButton.h" 

@protocol GAlleryScrollDelegate; 

@interface GalleryScrollView : UIView <GalleryButtonDelegate> 

{ 

id <GAlleryScrollDelegate> delegate; 

// MAIN WINDOW WHERE YOU CAN DRAG ICONS 
UIView *mainView; 

UIScrollView *_scrollView; 
NSMutableArray *_attachments; 


NSInteger *_totalSize; 

UIImageView *_recycleBin; 
CGRect recycleBinFrame; 
} 
@property (nonatomic, retain) id <GAlleryScrollDelegate> delegate; 

@property (nonatomic, retain) UIView *mainView; 
@property (nonatomic, retain) NSMutableArray *attachments; 

@property (nonatomic, retain) UIImageView *recycleBin; 
@property (nonatomic) CGRect recycleBinFrame; 

- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName; 
- (void) removeAttachment:(GalleryButton *)button; 
- (void) reloadData; 

@end 

// EVENTS IF YOU WANT TO DISABLE SOME SCROLL ON DID PRESS AND ENABLE IT ON DROP 
@protocol GAlleryScrollDelegate 
- (void) didPressButton; 
- (void) didDropButton; 
@end 

GalleryScrollView.m

#import <QuartzCore/QuartzCore.h> 
#import "GalleryScrollView.h" 
#import "GalleryButton.h" 

@implementation GalleryScrollView 


@synthesize delegate; 
@synthesize mainView; 
@synthesize attachments = _attachments; 
@synthesize recycleBin = _recycleBin, recycleBinFrame; 


int padding = 0; 


#pragma mark - INIT 

- (id) init 
{ 
self = [super init]; 
if (self) { 
    // Initialization code here. 
} 
return self; 
} 

- (id) initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self){ 
    ; 
} 
return self; 
} 

- (void) awakeFromNib 
{ 
// INIT ATTACHMENT ARRAY 
if (_attachments == nil){ 
    _attachments = [[NSMutableArray alloc] init]; 
} 

// SCROLL VIEW 
UIScrollView *scrollTemp = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-0, 450)]; 
_scrollView = scrollTemp; 
_scrollView.backgroundColor = [UIColor clearColor]; 

// RECYCLE BIN 
UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mozambique-wenge.png"]]; 
self.recycleBin = imageViewTemp; 
self.recycleBin.frame = CGRectMake(0, 0, 320, 270); 

[self addSubview:_scrollView]; 
[self addSubview:self.recycleBin]; 
[scrollTemp release]; 
[imageViewTemp release]; 
} 

- (void) dealloc { 
[super dealloc]; 

} 

#pragma mark - ATTACHMENTS ADD/REMOVE 

- (void) addAttachment:(AttachmentItem *)attachment withImageNamed:(NSString *)imgName 
{ 
// SAVE ATTACHMENT 
[_attachments addObject:attachment]; 

// RESIZE CONTENT VIEW FOR INSERTINT NEW ATTACHMENT 
_scrollView.contentSize = CGSizeMake([_attachments count]*70, 70); 

CGFloat startX = (70.0f * ((float)[_attachments count] - 1.0f) + padding); 
CGFloat startY = 370; 
CGFloat width = 64; 
CGFloat height = 64; 

GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName]; 
btnAttachment.tag = [_attachments count]; 
btnAttachment.scrollParent = _scrollView; 
btnAttachment.mainView = self.mainView; 
btnAttachment.delegate = self; 

if (attachment.type == 1){ 
}else if (attachment.type == 2){ 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)]; 
    imageView.image=[UIImage imageNamed:@"mozambique-wenge"]; 
    [btnAttachment addSubview:imageView]; 
    [imageView release]; 
} else if (attachment.type == 3){ 
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)]; 
    imageView.image=[UIImage imageNamed:@"recyclebin.png"]; 
    [btnAttachment addSubview:imageView]; 
    [imageView release]; 
} 

[_scrollView addSubview:btnAttachment]; 
[btnAttachment release]; 

} 


- (void) removeAttachment:(GalleryButton *)button 
{ 

} 

#pragma mark - RELOAD DATA 

- (void) reloadData 
{ 

} 

#pragma mark - GALLERY BUTTON DELEGATE 

-(void) touchDown 
{ 
[self.delegate didPressButton]; 
} 

-(void) touchUp 
{ 
[self.delegate didDropButton]; 
_scrollView.scrollEnabled = YES; 
} 

-(BOOL) isInsideRecycleBin:(GalleryButton *)button touching:(BOOL)finished; 
{ 
CGPoint newLoc = [self convertPoint:self.recycleBin.frame.origin toView:self.mainView]; 
CGRect binFrame = self.recycleBin.frame; 
binFrame.origin = newLoc; 

if (CGRectIntersectsRect(binFrame, button.frame) == TRUE){ 
    if (finished){ 
     [self removeAttachment:button]; 
    } 
    return YES; 
} 
else { 
    return NO; 
} 

} 

@end 

GalleryButton.m

#import <QuartzCore/QuartzCore.h> 
#import "GalleryButton.h" 
#import "GalleryScrollView.h" 

@implementation GalleryButton 

@synthesize delegate; 
@synthesize originalPosition = _originalPosition; 
@synthesize mainView, scrollParent; 
@synthesize images; 

- (id)init 
{ 
self = [super init]; 
if (self) { 
    // Initialization code here. 
} 

return self; 
} 

- (id)initWithFrame:(CGRect)frame imageName:(NSString *)imgName 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    isInScrollview = YES; 

    CGRect myImageRect = CGRectMake(0, 0, 64, 64); 
    images = [[UIImageView alloc] initWithFrame:myImageRect]; 

    // here's the change: 
    // instead of a constant name, use the `imgName` parameter 
    [images setImage:[UIImage imageNamed:imgName]]; 
    [self addSubview:images]; 

    self.backgroundColor = [UIColor blackColor]; 
    self.layer.borderWidth = 2; 
    self.layer.borderColor = [UIColor blackColor].CGColor; 
    self.layer.masksToBounds = YES; 
    self.layer.cornerRadius = 5; 
} 
return self; 
} 
#pragma mark - DRAG AND DROP 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

[self.delegate touchDown]; 
self.originalPosition = self.center; 
self.scrollParent.scrollEnabled = NO; 

if (isInScrollview == YES) { 
    CGPoint newLoc = CGPointZero; 
    newLoc = [[self superview] convertPoint:self.center toView:self.mainView]; 
    _originalOutsidePosition = newLoc; 

    //  [self.superview touchesCancelled:touches withEvent:event]; 
    [self removeFromSuperview]; 

    self.center = newLoc; 
    [self.mainView addSubview:self]; 
    [self.mainView bringSubviewToFront:self]; 
    isInScrollview = NO; 
} 
else { 
    ; 
} 

} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

[UIView beginAnimations:@"stalk" context:nil]; 
[UIView setAnimationDuration:.001]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

UITouch *touch = [touches anyObject]; 
self.center = [touch locationInView: self.superview]; 

[UIView commitAnimations]; 

if ([delegate isInsideRecycleBin:self touching:NO]){ 

} 

} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

if ([delegate isInsideRecycleBin:self touching:YES]){ 

    CGRect myImageRect = CGRectMake(0, 0, 320, 300); 
    images = [[UIImageView alloc] initWithFrame:myImageRect]; 
    [images setImage:[UIImage imageNamed:@"light-cherry.png"]]; 
    [self.mainView addSubview:images]; 

    UIImageView * animation = [[UIImageView alloc] init]; 
    animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40); 

    animation.animationImages = [NSArray arrayWithObjects: 
           [UIImage imageNamed: @"iconEliminateItem1.png"], 
           [UIImage imageNamed: @"iconEliminateItem2.png"], 
           [UIImage imageNamed: @"iconEliminateItem3.png"], 
           [UIImage imageNamed: @"iconEliminateItem4.png"] 
           ,nil]; 
    [animation setAnimationRepeatCount:1]; 
    [animation setAnimationDuration:0.35]; 
    [animation startAnimating]; 
    [self.mainView addSubview:animation]; 
    [animation bringSubviewToFront:self.mainView]; 
    [animation release]; 
    ; 
    [UIView beginAnimations:@"goback" context:nil]; 
    [UIView setAnimationDuration:0.4f]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.center = _originalOutsidePosition; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)]; 
    //  loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height); 
    [UIView commitAnimations]; 

} else{ 
    [UIView beginAnimations:@"goback" context:nil]; 
    [UIView setAnimationDuration:0.4f]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.center = _originalOutsidePosition; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)]; 
    //  loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height); 
    [UIView commitAnimations]; 


} 

[self.delegate touchUp]; 

} 

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
if ([animationID isEqualToString:@"goback"] && finished) { 
    [self removeFromSuperview]; 
    self.center = _originalPosition; 
    [self.scrollParent addSubview:self]; 
    isInScrollview = YES; 
} 
} 

@end 

回答

1

我認爲你在這裏面臨的唯一問題是你沒有聲明你的- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName方法。確保你已經在頭文件中聲明瞭這個方法:GalleryButton.hsignature相同。

+0

現在它給了我一個警告GalleryButton可能不會響應iniWithFrame:imageNamed?任何原因爲什麼後我在我的頭文件中聲明 - (id)initWithFrame:(CGRect)frame imageName:(NSString *)imageName; –

+0

你是否聲明你是一個GalleryButton或一個UIButton嗎?如果它是一個GalleryButton你不應該有這個警告。你不會忽略這個警告,因爲你的方法可能不會被調用 – tiguero

0

只是檢查GalleryButton.m是否包含initWithFrame方法。另外,請檢查您是否在您的GalleryButton.h類中聲明瞭方法簽名。

1

編輯initWithFrame方法

- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName 
    { 
    self = [super initWithFrame:frame]; 
    if (self){ 
     ; 
    } 
    return self; 
    } 

不要忘記在.h文件中聲明該方法以及

0

這將是相當有用檢查GalleryButton類,因爲這個問題是這個:

GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height) imageName:imgName]; 

但是你似乎沒有在那個類上實現該方法。