2009-11-15 81 views
0

我无法显示指标视图。如何显示指标

ItemController.h

#import <UIKit/UIKit.h> 


@interface ItemController : UITableViewController { 
    UIView* loadingView; 
    UIActivityIndicatorView* indicator; 
} 

@property (nonatomic, retain) UIView *loadingView; 
@property (nonatomic, retain) UIActivityIndicatorView *indicator; 

@end 

ItemController.m

..... 
- (void)netAccessStart { 


    loadingView = [[UIView alloc] initWithFrame:[[self view] bounds]]; 
    [loadingView setBackgroundColor:[UIColor blackColor]]; 
    [loadingView setAlpha:0.5]; 
    indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    [[self view] addSubview:loadingView]; 
    [loadingView addSubview:indicator]; 
    [indicator setFrame:CGRectMake ((320/2)-20, (480/2)-60, 40, 40)]; 
    [indicator startAnimating]; 

} 

- (void)netAccessEnd { 


    [indicator stopAnimating]; 
    [loadingView removeFromSuperview]; 

} 

- (void)dealloc { 
    [loadingView release]; 
    [indicator release]; 
    [super dealloc]; 
} 
..... 

继承类

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
    [self netAccessStart]; 
    sleep(1); 
    [self netAccessEnd]; 
} 

回答

1

sleep()块执行,这意味着你的应用程序被冻结在-viewWillAppear呼叫,在其中你的loadingView从它的sup删除erview。换句话说,在[self netAccessStart];[self netAccessEnd];之间没有绘图。我假设你调用一个立即另一个用于测试后的目的,在这种情况下,我的-netAccessStart命令移到-viewDidAppear:并用以下替换sleep/-netAccessEnd电话:

[self performSelector:@selector(netAccessEnd) withObject:nil afterDelay:1]; 

...这将具有相同的效果,但不会阻止执行。

0

这是因为你使用sleep,而不是真的在做事情。据推测,你的意思是从互联网上取回一些东西;做到这一点,并且只有在连接完成或失败时才发送自己的消息netAccessEnd

与延迟的另一种形式的仍然使用硬编码的间隔(例如,1秒)的更换sleep是一个好主意。如果时间少于1秒,则会让用户等待的时间超过必要的时间。如果花费时间超过1秒,那么在实际完成用户等待的任何操作之前,您都需要关闭指示器。

你需要采取的指标下降时,无论用户正在等待完成,不早,不晚。