以编程方式为UITableViewController设置UIActivityIndi​​catorView

我有一个普通的UITableViewController和一个UITableView作为唯一的视图,我想除了表视图之外还有一个UIActivittyIndicatorView

所以我需要一个像这样的视图结构:

 view (UIView): tableView activityIndicatorView 

没有InterfaceBuilder的最干净的方法是什么? 我想我需要重载的loadView:方法,但我没有成功做到目前为止。

ARC和iOS 5.0+更新(我认为旧版本需要被删除,因为我们有新的,更好的API :)):

添加到您的UIViewController子类的头文件.h文件中:

  @property (nonatomic, weak) UIActivityIndicator *activityIndicator; 

并覆盖UIViewController子类的.m文件中的方法:

 - (void)loadView { [super loadView]; UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; // If you need custom color, use color property // activityIndicator.color = yourDesirableColor; [self.view addSubview:activityIndicator]; [activityIndicator startAnimating]; self.activityIndicator = activityIndicator; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; CGSize viewBounds = self.view.bounds; self.activityIndicator.center = CGPointMake(CGRectGetMidX(viewBounds), CGRectGetMidY(viewBounds)); } 

================================================== ===========

非ARC版本,iOS <5.0:

你应该重写方法

 -(void)loadView { [super loadView]; self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease]; [self.view addSubview:self.activityIndicator]; self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2); [self.activityIndicator startAnimating]; } 

另外,添加

 @property (nonatomic, assign) UIActivityIndicatorView *activityIndicator; 

在头文件

 @synthesize activityIndicator; 

到.m文件