2013-01-25 46 views
1

我有一個進度條在我的應用程序顯示百分比值。iOS - 進度條值從php

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"; 

這可能嗎?如何做呢?

+2

「我在Xcode中有一個進度條」 - 而不是在你的應用程序中,對吧?這與Xcode沒有任何關係。 – 2013-01-25 14:13:09

+0

它在我的應用程序是的。我編輯了這篇文章,現在你可以看到整個代碼 – Stumpp

+0

你需要創建一個NSURLConnection來創建一個請求到您的PHP來獲取將在你的進度條中使用的值。 – Jeremy1026

回答

2

你必須建立與服務器E連接檢索值,以下樣本COSE

的test.php包含此指令:

<?php echo 40; ?> 

這裏發展觀爲樣本viewController.m通過接口連接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 
+0

謝謝你的回答!嗯..當我使用這段代碼時,我可以建立沒有錯誤的項目,但當它彈出時它會崩潰。這個問題似乎是在[[recievedData appendData:data];'它說'線程1:EXC_BAD_ACCESS(代碼= 1,地址= 0xe0741af7)「我不知道這是什麼意思 – Stumpp

+0

您是否定義了h文件中的recievedData?你分配了接收的數據嗎?我在Xcode中試過這段代碼,它工作正常。更好地檢查你的代碼 –