0
我customed一個UIView的iOS版:的UIView不顯示
@implementation PaiLifePageDetail
@synthesize pageIndex = _pageIndex;
#define MARGINTOTOP 200.0
#define MARGINTOLEFT 20.0
#define WIDTH 120.0
#define HEIGHT 150.0
- (id) initWithDataSource : (NSArray *) dataSource
{
self = [super init];
if (self)
{
if (dataSource)
{
for (PaiLifeCustomImageView *imageView in dataSource)
{
//page 1 line 1
if (_pageIndex == 1)
{
if ([imageView.location isEqualToString:@"r11"])
{
imageView.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH, HEIGHT * 2);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r12"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:imageView];
}
}
//not page 1 line 1
else
{
if ([imageView.location isEqualToString:@"r11"])
{
imageView.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r12"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r13"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP, WIDTH, HEIGHT);
[self addSubview:imageView];
}
}
// line 2
if ([imageView.location isEqualToString:@"r21"])
{
imageView.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r22"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r23"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + HEIGHT + 20, WIDTH, HEIGHT);
[self addSubview:imageView];
}
// line 3
if ([imageView.location isEqualToString:@"r31"])
{
imageView.frame = CGRectMake(MARGINTOLEFT, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r32"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + WIDTH + 20, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:imageView];
}
else if ([imageView.location isEqualToString:@"r33"])
{
imageView.frame = CGRectMake(MARGINTOLEFT + (WIDTH + 20) * 2, MARGINTOTOP + (HEIGHT + 20) * 2, WIDTH, HEIGHT);
[self addSubview:imageView];
}
}
}
}
[self setNeedsDisplay];
return self;
}
@end
這是我的看法
implementation PaiLifeViewController
@synthesize detail = _detail;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *i = [UIImage imageNamed:@"menu_patcode_normal"];
PaiLifeCustomImageView *c1 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c1.location = @"r11";
PaiLifeCustomImageView *c2 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c2.location = @"r12";
PaiLifeCustomImageView *c4 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c4.location = @"r21";
PaiLifeCustomImageView *c5 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c5.location = @"r22";
PaiLifeCustomImageView *c6 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c6.location = @"r23";
PaiLifeCustomImageView *c7 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c7.location = @"r31";
PaiLifeCustomImageView *c8 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c8.location = @"r32";
PaiLifeCustomImageView *c9 = [[PaiLifeCustomImageView alloc] initWithImage:i];
c9.location = @"r33";
NSArray *c = [[NSArray alloc] initWithObjects:c1,c2,c4,c5,c6,c7,c8,c9, nil];
//PaiLifePageDetail *pagec
_detail.pageIndex = 1;
_detail= [[PaiLifePageDetail alloc] initWithDataSource:c];
[_detail awakeFromNib];
}
@end
但它不顯示,我不知道爲什麼,幫助我。謝謝。
看來你從來沒有您的視圖添加到您的視圖控制器,你呢? – Matthias 2013-02-25 12:35:04
@Matthias我在IB中將我的視圖添加到我的視圖控制器。 – jxdwinter 2013-02-25 12:55:45
那麼,根據你的代碼,你的'detail'對象是在'viewDidLoad'中創建的,也就是爲了延遲NIB自動化。如果你想從NIB加載一個自定義視圖,你必須重寫'initWithCoder:',c.f. [Apple's guide](http://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW23)。 – Matthias 2013-02-25 13:19:07