1

我的应用程序中有一个进度条显示百分比值。

progressView = [[DDProgressView alloc] initWithFrame:CGRectMake(20.0f, 140.0f, self.view.bounds.size.width - 40.0f, 0.0f)];
[progressView setOuterColor:[UIColor grayColor]];
[progressView setInnerColor:[UIColor lightGrayColor]];
[self.view addSubview:progressView];
[progressView release];
float randomPercentageFloat = 40;
progressView.progress = randomPercentageFloat / 100;

这显示了一个已满 40% 的进度条。我想让进度条显示来自 php 的值。我有一个回显一个值的 php 文件。我想将其更改randomPercentageFloat = 40;为类似

float randomPercentageFloat = "www.website.com/value.php";

这可能吗?怎么做到呢?

4

1 回答 1

2

您必须与您的服务器建立连接并按照示例 cose 检索值

test.php 包含以下指令:

<?php echo 40; ?>

这里的示例 viewController.m 带有通过 Interface Builder 链接的进度视图

#import "ViewController.h"

@interface ViewController () {
    IBOutlet UIProgressView *progressView;

    NSMutableData *receivedData;
}

@end

@implementation ViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    progressView.progressTintColor = [UIColor lightGrayColor];
    progressView.trackTintColor = [UIColor grayColor];
    progressView.progress = 0.0f;

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.29/test.php"]
                     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                 timeoutInterval:10.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
        UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad"  delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [connectFailMessage show];
    } 

}

#pragma mark NSURLConnection Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    receivedData = [NSMutableData data];
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{


    // inform the user
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *dataString = [[NSString alloc] initWithData: receivedData  encoding:NSUTF8StringEncoding];
    progressView.progress = [dataString floatValue] / 100;
}


@end
于 2013-01-25T15:09:55.573 回答