Controller设置UIActivityIndi​​cato

Controller设置UIActivityIndi​​cato

本文介绍了以编程方式为UITableViewController设置UIActivityIndi​​catorView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个常规的UITableViewController和一个UITableView作为其唯一视图,并且除了表视图之外,我还想要一个UIActivittyIndicatorView.

I have a regular UITableViewController and a UITableView as its only view, and I want to have an UIActivittyIndicatorView in addition to the table view.

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

So I need a view structure like this:

view (UIView):
  tableView
  activityIndicatorView

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

What's the cleanest way to do it without InterfaceBuilder? I guess I need to override the loadView: method, but I haven't succeed doing it so far.

推荐答案

用于ARC和iOS 5.0+的更新(我认为,由于我们拥有新的更好的API,因此旧版本必须已经删除:))

UPDATE for ARC and iOS 5.0+ (I think old version needs to be removed already as we have new, better API's:)):

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

Add to header .h file of your UIViewController subclass:

 @property (nonatomic, weak) UIActivityIndicator *activityIndicator;

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

And override methods in .m file of your UIViewController subclass:

- (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:

non-ARC version, 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文件

这篇关于以编程方式为UITableViewController设置UIActivityIndi​​catorView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:03