我对故事板中的UIScrollView有一个奇怪的问题。我创建了自定义UITableViewCell,并希望在其中包含UIScrollView。当我用代码做它的时候,它就像一种魅力。我就是这样做的:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 25, 650, 25)];
scroller.showsHorizontalScrollIndicator = NO;
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
contentLabel.backgroundColor = [UIColor blackColor];
contentLabel.textColor = [UIColor whiteColor];
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
[str appendFormat:@"%i ", i];
}
contentLabel.text = str;
[scroller addSubview:contentLabel];
scroller.contentSize = contentLabel.frame.size;
[self addSubview:scroller];
}
return self;
}但是,当我在故事板中创建UIScrolView并放入我的原型单元格中时,将出口连接到customCell类,然后完全相同:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
storyboardScrollView.showsHorizontalScrollIndicator = NO;
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
contentLabel.backgroundColor = [UIColor blackColor];
contentLabel.textColor = [UIColor whiteColor];
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
[str appendFormat:@"%i ", i];
}
contentLabel.text = str;
[storyboardScrollView addSubview:contentLabel];
storyboardScrollView.contentSize = contentLabel.frame.size;
}
return self;
}我的手机是空的。你知道这是怎么回事吗?
发布于 2014-03-21 08:04:31
在- (instancetype)initWithCoder:(NSCoder *)aDecoder函数中,IBOutlets尚未与类连接。因此,当调用nil函数时,指针将具有- (void)awakeFromNib,它们将被连接。
所以你可以这样做。
- (void)awakeFromNib
{
storyboardScrollView.showsHorizontalScrollIndicator = NO;
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
contentLabel.backgroundColor = [UIColor blackColor];
contentLabel.textColor = [UIColor whiteColor];
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < 100; i++)
{
[str appendFormat:@"%i ", i];
}
contentLabel.text = str;
[storyboardScrollView addSubview:contentLabel];
storyboardScrollView.contentSize = contentLabel.frame.size;
}https://stackoverflow.com/questions/22553009
复制相似问题