我过去这样做的方式是拥有一个内容视图,其中包含活动视图或适当的视图。
在视图控制器的 nib 中,不要将子视图添加到主视图,而是将其留空并为视图创建一个新视图(例如下面示例中的表格视图)。
还要创建一个活动视图(带有线程进度指示器或类似的)和“无结果”视图。
然后从以下内容派生您的控制器类:
//
// ContainerViewController.h
//
#import <UIKit/UIKit.h>
@interface ContainerViewController : UIViewController
{
UIView *myContainerView;
UITableView *myTableView;
UIView *mySearchActivityView;
UIView *myZeroResultsView;
UIView *myCurrentlyShowingView;
}
@property (retain, nonatomic) IBOutlet UIView *containerView;
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (retain, nonatomic) IBOutlet UIView *searchActivityView;
@property (retain, nonatomic) IBOutlet UIView *zeroResultsView;
@property (assign) UIView *currentlyShowingView;
@end
//
// ContainerViewController.m
//
#import "ContainerViewController.h"
@implementation ContainerViewController
@synthesize containerView = myContainerView;
@synthesize tableView = myTableView;
@synthesize searchActivityView = mySearchActivityView;
@synthesize zeroResultsView = myZeroResultsView;
- (void)dealloc
{
[myContainerView release], myContainerView = nil;
[myTableView release], myTableView = nil;
[mySearchActivityView release], mySearchActivityView = nil;
[myZeroResultsView release], myZeroResultsView = nil;
myCurrentlyShowingView = nil;
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentlyShowingView = mySearchActivityView;
mySearchActivityView.backgroundColor = [UIColor clearColor];
myZeroResultsView.backgroundColor = [UIColor clearColor];
}
- (void)setCurrentlyShowingView:(UIView *)view
{
[myCurrentlyShowingView removeFromSuperview];
CGRect frame = view.frame;
frame.size = myContainerView.frame.size;
view.frame = frame;
[myContainerView addSubview:view];
myCurrentlyShowingView = view;
if (view == myTableView)
[myTableView reloadData];
}
- (UIView *)currentlyShowingView
{
return myCurrentlyShowingView;
}
@end
并在派生类的-viewDidLoad方法中,触发(异步)查询:
- (void)viewDidLoad
{
[super viewDidLoad];
myQueryLoader = [[QueryLoader alloc] initWithQuery:@"whatever" delegate:self];
self.currentlyShowingView = mySearchActivityView;
}
在委托回调中:
- (void)queryLoader:(QueryLoader *)queryLoader didEndWithResults:(id)results error:(NSError *)error
{
myItems = [results retain];
if (myItems)
self.currentlyShowingView = myTableView;
else
self.currentlyShowingView = myZeroResultsView;
}
希望这可以帮助!