1

我正在使用来自 GitHub 的 MBProgressHUD,并希望将正在运行的进度浮动传递给另一个类。

在 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”

NS日志:

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

希望你能帮忙!提前致谢!:)

4

4 回答 4

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];
}
于 2013-02-27T13:31:51.153 回答
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 AKey-Value 观察。classB.progress您可以在 init 中执行此操作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
}
于 2013-02-27T14:13:03.007 回答
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];
}
于 2013-02-27T13:29:38.300 回答
0

您可以使用以下任何一种:

  1. 使变量全局

  2. 静止的

  3. 单身人士

  4. 作为参数传递

  5. 作为通知发布。

在这里检查。

注意:由于新程序员发现全局变量在列表顶部很容易,一旦他们习惯了 obj-c,他们就知道哪个是最好的以及何时。

于 2013-02-27T13:29:57.310 回答