我的应用程序中有一个显示百分比值的进度条。
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";
这可能吗?怎么做到的?
最佳答案
您必须按照示例步骤与您的服务器建立连接并检索值
test.php 包含这条指令:
<?php echo 40; ?>
此处示例 viewController.m 带有通过 Interface Builder 链接的进度 View
#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
关于iOS - 带有来自 php 的值的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14523354/