【发布时间】:2014-03-18 11:21:37
【问题描述】:
我创建了一个简单的 ProgressView 类,我希望在执行一些长任务时显示该类。但是,当我将它添加到父视图时,它永远不会变得可见,并且永远不会调用 layoutSubviews。
类实现如下:
@implementation ProgressView
@synthesize title;
@synthesize cancel;
@synthesize progress;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
//frame = CGRectMake( 0, 0, 240, 112 );
self = [super initWithFrame:frame];
if ( self )
{
self.title = [[UILabel alloc] init];
self.cancel = [UIButton buttonWithType: UIButtonTypeCustom];
self.progress = [[UIProgressView alloc] init];
self.backgroundColor = [UIColor darkGrayColor];
[self addSubview: self.title];
[self addSubview: self.cancel];
[self addSubview: self.progress];
}
return self;
}
+ (id) createWithTitle: (NSString*) pTitle
{
ProgressView* pRet = [[ProgressView alloc] initWithTitle: pTitle];
return pRet;
}
- (id) initWithTitle: (NSString*) pTitle
{
self = [super initWithFrame: CGRectMake( 0, 0, 240, 112 )];
if ( self )
{
self.title.text = pTitle;
}
return self;
}
- (void) layoutSubviews
{
CGSize superFrameSize = self.superview.frame.size;
CGSize halfSuperFrameSize = CGSizeMake( superFrameSize.width / 2, superFrameSize.height / 2 );
CGSize frameSize = self.frame.size;
CGSize halfFrameSize = CGSizeMake( frameSize.width / 2, frameSize.height / 2 );
// Center the window on its parent.
[self setFrame: CGRectMake( halfSuperFrameSize.width - halfFrameSize.width, halfSuperFrameSize.height - halfFrameSize.height, frameSize.width, frameSize.height )];
[self.title setFrame: CGRectMake( 4, 4, frameSize.width - 8, 24 )];
[self.progress setFrame: CGRectMake( 4, 32, frameSize.width - 8, 24 )];
[self.cancel setFrame: CGRectMake( 4, 64, frameSize.width - 8, 44 )];
[self setNeedsDisplay];
}
我添加如下视图:
pProgressView = [[ProgressView alloc] initWithTitle: @"Downloading..."];
[pProgressView setDelegate: self];
[self.view addSubview: pProgressView];
但它从不显示。我已经检查过删除肯定会在以后发生,并且有足够的时间让视图变得可见。然而我不知所措。无论我做什么,ProgressView 都不会显示。
有人能指出我犯的最可能是愚蠢的错误吗?
【问题讨论】:
-
您在哪里添加您的视图?问题可能来自这里
-
@NicolasBonnet:谢谢 Nicolas,但我不认为我这样做有什么奇怪的。我是在委托调用内部进行的,我已经确认这发生在主线程上。
-
我也确认过几秒后,当下载完成时,pProgressView.superview 和 "self.view" 一样...
-
是不是你的主线程在你添加这个视图之后就被阻塞了,这就是它没有出现的原因?我假设您在添加
pProgressView后正在做某事。尝试将“某物”移至辅助线程。 -
@Levi:不......因为 UIListView 视图应该出现在它上面仍然响应......
标签: ios iphone objective-c uiview uikit