iOS - 带有来自 php 的值的进度条

标签 ios objective-c cocoa-touch

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

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/

相关文章:

iphone - Json在iphone中传递

objective-c - Swift Extension 无法向 Objective-C 类添加重载方法

objective-c - QuartzCore - 在 iOS8 中崩溃

ios - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb) on uitextfield

iphone - NSData 表现为可消耗的数据流。可能的?

ios - 尝试在 Swift 中访问嵌套字典数组中的键

ios - 计算距离,转换为公里,然后去掉小数位

iphone - 为 iPhone 的 UIButton 添加特定的触摸事件

ios - IOS实现Oauth2.0流程

iphone - iPhone 应用程序中的数据持久性