2013-02-27 30 views
1

我正在使用來自GitHub的MBProgressHUD並想將正在運行的進程float傳遞給另一個類。將正在運行的float傳遞給另一個類

在A類:

-(void)methodName 
{ 
    HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.navigationController.view addSubview:HUD]; 
    HUD.mode = MBProgressHUDModeAnnularDeterminate; 
    HUD.delegate = self; 
    HUD.dimBackground = YES; 
    HUD.labelText = @"Loading"; 
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES]; 
} 
- (void)myProgressTask 
{ 
    HUD.progress = progress; 
} 

在B類:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    expectedLength = [response expectedContentLength]; 
    currentLength = 0; 
    HUD.mode = MBProgressHUDModeDeterminate; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    currentLength += [data length]; 
    float progress = currentLength/(float)expectedLength; 
    NSLog(@"%f", progress); 
} 

需要通過 「浮進度」 從B類A類的方法: 「myProgressTask」

的NSLog :

2013-02-27 15:23:14.006 [8209:c07] 0.161726 
2013-02-27 15:23:14.329 [8209:c07] 0.253171 
2013-02-27 15:23:14.718 [8209:c07] 0.436063 
2013-02-27 15:23:15.941 [8209:c07] 0.527508 
2013-02-27 15:23:16.230 [8209:c07] 0.618954 
2013-02-27 15:23:16.238 [8209:c07] 0.710400 
2013-02-27 15:23:16.614 [8209:c07] 0.893291 
2013-02-27 15:23:16.615 [8209:c07] 0.984736 
2013-02-27 15:23:16.618 [8209:c07] 1.000000 

希望你能幫助!提前致謝! :)

+1

投票關閉,有輕微變化類似的問題已經存在http://stackoverflow.com/questions/15063078/how-to-access-一個屬性的視圖控制器從另一個iphone/15063118#15063118 – 2013-02-27 13:31:28

+1

該問題是沒有類似於我的... – emotality 2013-02-27 13:41:04

+1

相似性正在傳遞價值從一個類到其他。不是嗎? – 2013-02-27 13:41:55

回答

1

更改此方法:

- (void)myProgressTask:(float)progress 
{ 
    HUD.progress = progress; 
} 

在你的B類變化:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    currentLength += [data length]; 
    float progress = currentLength/(float)expectedLength; 
    NSLog(@"%f", progress); 
    ClassA obj = [[ClassA alloc] init]; //Put autorelease if you are not using ARC. 
    [obj myProgressTask:progress]; 
} 
+0

這感覺像正確的方式,並且更容易,但它不工作:( – emotality 2013-02-27 14:26:28

+0

我用這個,並找出它!謝謝男人!得到它運行,只需要把它放在正確的位置:) – emotality 2013-02-27 14:54:42

0

最好的解決方案是使用協議。見XCode中實用應用

的4例如在A類首標:

@interface ClassA : UIViewController <ClassBDelegate> 

@property float yourFloat; 

在A類實現:

- (void)loadClassB 
{ 
    ClassB *classB = [ClassB new]; 
    classB.delegate = self; 
} 

B類頭文件:

@class ClassB; 

@protocol ClassBDelegate 
- (void)callbackMethod; 
@end 

@interface ClassB : UIViewController 

@property (weak, nonatomic) id <ClassBDelegate> delegate; 



@end 

類B實施:

- (void)viewDidLoad 
{ 
    float newFloat = self.delegate.yourFloat; // <- the assignment occurs here 
    [self.delegate callbackMethod]; 
} 
+0

這是沒有道理的。請舉例? – emotality 2013-02-27 13:33:19

+0

查看編輯答案 – pbibergal 2013-02-27 13:55:55

+0

在你的情況下,你不應該傳遞變量。您必須通過代理 – pbibergal 2013-02-27 14:03:47

0

您可以使用下面的任何一個:

  1. 使變量全球

  2. 靜態

  3. 作爲參數傳遞

  4. 作爲通知發佈。

檢查here

注意:由於新程序員在列表頂部列出的全局變量很容易找到,一旦他們習慣了obj-c,他們就知道哪個是最好的以及何時。

+0

您可以請演示或指導我如何做1? – emotality 2013-02-27 13:31:45

+2

Pleeease避免全局性。 – pbibergal 2013-02-27 13:31:48

+0

因爲新的程序員覺得很容易,一旦他們習慣了obj-c,他們知道什麼時候是最好的,什麼時候最好。 :) – 2013-02-27 13:32:47

0

有很多方法來解決你的問題。我會用Key-Value Observing

品牌progress財產Class B。然後改變:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    currentLength += [data length]; 
    //float progress = currentLength/(float)expectedLength; 
    self.progress = currentLength/(float)expectedLength; 
    //NSLog(@"%f", progress); 
    NSLog(@"%f", self.progress); 
} 

class A然後註冊爲classB.progress鍵值觀察。您可以在初始化class A中執行此操作。

[classB_object_reference addObserver:self 
          forKeyPath:@"progress" 
          options:NSKeyValueObservingOptionNew 
          context:nil]; 

添加這個方法來class A

- (void)observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    //do whatever you want here with the new value of classB_object.progress 
}