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

サンプルコードに従って、サーバーとの接続を確立して値を取得する必要があります

test.php には次の命令が含まれています。

<?php echo 40; ?>

ここではInterface Builderを介してリンクされた進行状況ビューを持つサンプルviewController.m

#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 に答える