2012-03-23 7 views
8

Tôi có một thường xuyên UITableViewControllerUITableView như xem duy nhất của nó, và tôi muốn có một UIActivittyIndicatorView ngoài việc xem bảng.Thiết lập một UIActivityIndicatorView cho UITableViewController lập trình

Vì vậy, tôi cần một cấu trúc nhìn như thế này:

view (UIView): 
    tableView 
    activityIndicatorView 

cách sạch để làm điều đó mà không InterfaceBuilder là gì? Tôi đoán tôi cần phải ghi đè lên phương pháp loadView:, nhưng tôi đã không thành công làm điều đó cho đến nay.

Trả lời

27

CẬP NHẬT cho ARC và iOS 5.0+ (Tôi nghĩ rằng phiên bản cũ cần phải được loại bỏ đã vì chúng tôi có mới, tốt hơn API :)):

Thêm vào tiêu đề .h tập tin của UIViewController phân lớp của bạn:

@property (nonatomic, weak) UIActivityIndicator *activityIndicator; 

Và override phương pháp trong file .m của UIViewController phân lớp của bạn:

- (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)); 
} 

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

phi ARC phiên bản, iOS < 5.0:

Bạn nên ghi đè phương pháp

-(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]; 
} 

Ngoài ra, thêm

@property (nonatomic, assign) UIActivityIndicatorView *activityIndicator; 

ở file header

@synthesize activityIndicator; 

vào tệp .m

+0

Không nên là 'CGRect viewBounds = self.view.bounds;' thay vì 'CGSize viewBounds ...'? – wmora