Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios initWithCoder:在不导入类别的情况下调用_Ios_Objective C_Uiimageview_Nscoding - Fatal编程技术网

Ios initWithCoder:在不导入类别的情况下调用

Ios initWithCoder:在不导入类别的情况下调用,ios,objective-c,uiimageview,nscoding,Ios,Objective C,Uiimageview,Nscoding,UIImageView类别已在HDScrollview.h中定义。它不是在GoodDetailViewController.m中导入的,而是在代码cell=[[[NSBundle mainBundle]装入nibNamed:CellIdentifier所有者:self选项:nil]lastObject]时导入的运行时,将调用HDScrollview.m中的-(id)initWithCoder:(NSCoder*)aDecoder。 这是怎么回事?任何帮助都将不胜感激 附言 HDScrollvie

UIImageView类别已在
HDScrollview.h
中定义。它不是在
GoodDetailViewController.m
中导入的,而是在代码
cell=[[[NSBundle mainBundle]装入nibNamed:CellIdentifier所有者:self选项:nil]lastObject]时导入的运行时,将调用
HDScrollview.m
中的
-(id)initWithCoder:(NSCoder*)aDecoder
。 这是怎么回事?任何帮助都将不胜感激

附言

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;
}
...

@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

...
。。。
#导入“GoodDetailViewController.h”
...
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.row==\u mutableArrayGoods.count){
返回[self-setLoadMoreCell];
}
静态NSString*CellIdentifier=@“TableViewCellGoods”;
TableViewCellGoods*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[NSBundle mainBundle]loadNibNamed:CellIdentifier所有者:self选项:nil]lastObject];
[单元格初始化GoodsCellDefaultStyle];
}
//配置单元格。。。
if(indexPath.row<_mutableArrayGoods.count){
Good*Good=[\u mutableArrayGoods对象索引:indexPath.row];
cell.good=好;
cell.delegate=self;
}
返回单元;
}
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
#导入
#导入“HdPageControl.h”
@协议HDScrollviewDelegate
-(void)TapView:(int)索引;
@结束
@界面HDScrollview:UIScrollView
@属性(非原子,强)HdPageControl*pagecontrol;
@属性(非原子,赋值)NSInteger currentPageIndex;
@属性(赋值,非原子)id HDdelegate;
-(id)initWithFrame:(CGRect)frame withImageView:(NSMutableArray*)imageview;
-(id)initLoopScrollWithFrame:(CGRect)frame withImageView:(NSMutableArray*)imageview;
-(无效)HDscrollViewDidScroll;
-(无效)HDScrollViewDiEnd减速;
@结束
@界面UIImageView(CopyImageview)
@结束
HDScrollview.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;
}
...

@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

...
。。。
@实现UIImageView(CopyImageview)
-(void)编码器与编码器:(NSCoder*)一个编码器
{
[aCoder encodeObject:self.backgroundColor forKey:@“backgroundColor”];
[aCoder-encodeObject:self.image-forKey:@“image”];
[aCoder encodeInt:self.contentMode forKey:@“contentMode”];
[aCoder encodeInt:self.subviews.count forKey:@“subviewscont”];

对于(int i=0;i当您从Nib文件实例化视图对象时,iOS将使用Nib文件的XML详细信息并从中创建一个解码器。然后它将调用视图的
initWithCoder:
方法,以便您的视图可以使用解码器信息初始化其状态和属性。

您不需要显式导入类别,除非您向类别中添加新方法并在控制器中使用(需要导入,因为编译器知道该方法可用)。这里发生的情况是,您的Xib可能包含一个
UIImageView
,当加载Xib时,创建图像视图,这将导致调用
initWithCoder
,因为对象是从Xib实例化的(这对于从Xib实例化的所有对象都是通用的)。这里没有什么神奇的事情发生

但我没有导入类别,为什么要调用initWithCoder:get方法?只有在添加了新方法时才需要导入类别,如果尝试覆盖类别中的标准方法,它们将是iOS,iOS将调用类别中实现的方法,而不是调用其框架实现。的机制category看起来难以置信。category用于扩展现有类,例如添加/修改方法。所有更改都成为实际类的一部分。这意味着如果您通过更改现有方法来扩展UIImageView类,则该更改适用于您项目的所有UIImageView,我可以理解这一点,但它确实让我感到惊讶如果不导入它,您会感到不安全。我建议您不要使用category来重写方法,除非它确实是必要的(适用于所有人)。您可以将UIImageView子类化,并在需要时使用该类