如何显示指标

时间:2009-11-15 02:48:54

标签: iphone objective-c cocoa cocoa-touch

我无法显示指标视图。

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

2 个答案:

答案 0 :(得分:1)

sleep()阻止执行,这意味着您的应用在-viewWillAppear来电时冻结,最后您的loadingView从其超级视图中删除。换句话说,[self netAccessStart];[self netAccessEnd];之间没有绘图。我假设您是为了测试目的而紧接着调用一个 - 在这种情况下,我会将-netAccessStart命令移至-viewDidAppear:并替换sleep / -netAccessEnd请致电以下人员:

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

...具有相同的效果,但没有阻止执行。

答案 1 :(得分:0)

这是因为您使用sleep而不是实际做某事。据推测,你的意思是从互联网上检索一些东西;这样做,只有在连接完成或失败时才发送netAccessEnd消息。

用另一种仍然使用硬编码间隔(例如1秒)的延迟替换sleep是一个坏主意。如果时间不到1秒,则表示用户等待的时间超过了必要时间。如果花费的时间超过1秒,那么在实际完成用户正在等待的任何内容之前,您将关闭指示器。

当用户等待完成时,您需要关闭指示器 - 不久之后,不会更晚。