2010-09-20 19 views
-1

我需要显示在背景中在分钟的WebRequest加载时间的计时器:秒format.When所述的WebRequest成功加载定时器具有stop.My代码是在这里:如何在xcode中显示webrequest加载时间的NSTimer?

@interface TimerWithWebrequestViewController : UIViewController { 
    IBOutlet UIWebView *webView; 
    IBOutlet UILabel *lbl; 
    int mainInt; 
    NSTimer *randomMain; 
} 
@property(nonatomic,retain) UIWebView *webView; 
-(void)upDate; 
@end 

@implementation TimerWithWebrequestViewController 
@synthesize webView; 
-(void)viewDidLoad 
{ 
NSString *urlAdd = @"http://www.google.com"; 
NSURL *url = [NSURL URLWithString:urlAdd]; 
NSURLRequest *reqObj = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:reqObj]; 
[self performSelectorInBackground:@selector (upDate) withObject:nil]; 

} 
-(void)upDate 
{ 
mainInt = 0; 
randomMain = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES]; 

} 
-(void)randomMainVoid{ 
mainInt += 1; 
lbl.text = [NSString stringWithFormat:@"%d",mainInt]; 

} 
+0

不是问题。 – Nick 2010-09-20 06:07:30

+0

请勿在堆栈溢出中使用“代码”标签。改为使用“101010”按钮来格式化代码。 – 2010-09-20 06:16:45

回答

0

NSTimer不这个工作的正确工具,因为课程要求你指定一个时间间隔。你想要做的是运行一个“秒表”。当负载启动,您可以通过保存时间戳做到这一点,当它完成通过实现以下UIWebViewDelegate方法:

- (void)webViewDidStartLoad:(UIWebView *)webView; 
- (void)webViewDidFinishLoad:(UIWebView *)webView; 
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; 

您可以使用NSDate此时间戳(主叫[NSDate date]),使用time(3)功能等。考虑到两个时间戳的差异会给你你的间隔,也就是你的加载时间。

相关问题