2014-06-13 53 views
0

UIImageView類別已在HDScrollview.h中定義。它不是在GoodDetailViewController.m中導入的,但是當代碼cell = [[[NSBundle mainBundle] loadNibNamed: CellIdentifier owner: self options: nil] lastObject];運行時,將會調用-(id)initWithCoder:(NSCoder *)aDecoder中的HDScrollview.m。 這是怎麼回事?任何幫助將不勝感激。initWithCoder:無需導入即可調用類別

ps。

HDScrollview.h導入在GoodDetailViewController.m但不是在GoodDetailViewController.h

GoodsListViewController.m

... 
#import "GoodDetailViewController.h" 
... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == _mutableArrayGoods.count) { 
     return [self setLoadMoreCell]; 
    } 

    static NSString *CellIdentifier = @"TableViewCellGoods"; 
    TableViewCellGoods *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[NSBundle mainBundle] loadNibNamed: CellIdentifier owner: self options: nil] lastObject]; 
     [cell initGoodsCellDefaultStyle]; 
    } 

    // Configure the cell... 
    if (indexPath.row < _mutableArrayGoods.count) { 
     Good *good = [_mutableArrayGoods objectAtIndex: indexPath.row]; 
     cell.good = good; 
     cell.delegate = self; 
    } 

    return cell; 
} 

HDScrollview.h

#import <UIKit/UIKit.h> 
#import "HdPageControl.h" 
@protocol HDScrollviewDelegate <NSObject> 
-(void)TapView:(int)index; 
@end 

@interface HDScrollview : UIScrollView<UIScrollViewDelegate> 

@property (nonatomic,strong) HdPageControl *pagecontrol; 
@property (nonatomic,assign) NSInteger currentPageIndex; 
@property (assign,nonatomic) id<HDScrollviewDelegate> HDdelegate; 

-(id)initWithFrame:(CGRect)frame withImageView:(NSMutableArray *)imageview; 

-(id)initLoopScrollWithFrame:(CGRect)frame withImageView:(NSMutableArray *)imageview; 
-(void)HDscrollViewDidScroll; 
-(void)HDscrollViewDidEndDecelerating; 
@end 

@interface UIImageView (CopyImageview)<NSCoding> 
@end 

HDScrollview.m

... 

@implementation UIImageView (CopyImageview) 
-(void)encodeWithCoder:(NSCoder *)aCoder 
{ 

    [aCoder encodeObject:self.backgroundColor forKey:@"backgroundColor"]; 
    [aCoder encodeObject:self.image forKey:@"image"]; 
    [aCoder encodeInt:self.contentMode forKey:@"contentMode"]; 
    [aCoder encodeInt:self.subviews.count forKey:@"subviewscount"]; 
    for(int i=0;i<self.subviews.count;i++) 
    { 
     UIView *view=self.subviews[i]; 
     [aCoder encodeObject:view forKey:[NSString stringWithFormat:@"view%d",i]]; 
    } 
} 
-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (self=[super init]) { 
     self.backgroundColor=[aDecoder decodeObjectForKey:@"backgroundColor"]; 
     self.image=[aDecoder decodeObjectForKey:@"image"]; 
     self.contentMode=[aDecoder decodeIntForKey:@"contentMode"]; 
     int subviewscount=[aDecoder decodeIntForKey:@"subviewscount"]; 
     for(int i=0;i<subviewscount;i++) 
     { 
      UIView* view=[aDecoder decodeObjectForKey:[NSString stringWithFormat:@"view%d",i]]; 
      [self addSubview:view]; 
     } 
    } 
    return self; 
} 
@end 

... 

回答

1

除非您將新方法添加到類別並在控制器中使用它(因爲編譯器知道方法可用,所以需要導入),否則不需要明確導入類別。這裏發生了什麼是你的Xib可能包含一個UIImageView,而Xib被加載,該圖像視圖被創建,這將導致initWithCoder被調用,因爲該對象是從Xib實例化的(這對所有對象實例化廈門國際銀行)。這裏沒有魔法

+0

類別的機制似乎令人難以置信。 –

+0

類別用於擴展現有類,如添加/修改方法。所有的變化都成爲實際課堂的一部分。意味着如果通過更改現有方法擴展UIImageView類,該更改適用於項目的所有UIImageView's –

+0

我可以理解這一點,但是如果不導入它,會讓我感覺不安全。 –

1

當你實例從筆尖文件的iOS視圖對象將使用筆尖文件的XML細節,並會從它創建一個解碼器。然後它將調用視圖的initWithCoder:方法,以便您的視圖可以使用解碼器信息來初始化其狀態和屬性。

+0

但我沒有導入類,爲什麼initWithCoder方法被調用? –

+1

只有在添加新方法時才需要導入類別。如果嘗試在類別中覆蓋標準方法,則它們將調用在類別中實現的方法,而不是調用其框架實現。 – kkumpavat

相關問題