【问题标题】:How to display NSTimer for a webrequest loading time in xcode?如何在 xcode 中显示 NSTimer 的 webrequest 加载时间?
【发布时间】:2010-09-20 05:47:59
【问题描述】:

我需要在后台显示一个计时器,用于以分钟:秒格式的 webrequest 加载时间。当 webrequest 成功加载时,计时器必须停止。我的代码在这里:

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

}

【问题讨论】:

  • 不要在 Stack Overflow 上使用“代码”标签。改用“101010”按钮格式化代码。

标签: iphone uiwebview ios nstimer


【解决方案1】:

NSTimer 不是该作业的正确工具,因为该类要求您指定时间间隔。你想要做的是运行一个“秒表”。您可以通过实现以下UIWebViewDelegate 方法在加载开始和完成时保存时间戳来实现此目的:

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

您可以使用NSDate 作为此时间戳(调用[NSDate date]),使用time(3) 函数等。取两个时间戳的差值即可得出您的时间间隔,即加载时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多