MBProgressHUD没有从另一个类显示

时间:2014-07-29 18:24:15

标签: ios parse-platform uiactivityindicatorview mbprogresshud

我通过按下按钮从Parse Cloud获取值。从Parse获取数据数据的代码在另一个类中,但是当我尝试从该类显示MBProgessHUD时它没有做任何事情。

也没有错误。但是,如果我在同一个班级中完成所有工作,那就有效。不确定如何在当时打开的特定视图上显示另一个类的progressHUD。

- (IBAction)testWeather:(id)sender {

     Reports * obje2 = [[Reports  alloc]init];
     [obje2 startGetting];
}

//在报告类

-(void) startGetting {



           Names= [[NSMutableArray alloc] init];
        Latitude= [[NSMutableArray alloc] init];
        Longitude= [[NSMutableArray alloc] init];


    [Names removeAllObjects];
    [Latitude removeAllObjects];
    [Longitude removeAllObjects];


    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *CountyName = [standardUserDefaults stringForKey:@"CurrentlySelectedCounty"];


    PFQuery *query = [PFQuery queryWithClassName:@"Reports"];
    [query whereKey:@"County" equalTo:@"Universal"];


    //
    NSInteger countTotal= [query countObjects];
    NSString *totalVals = [NSString stringWithFormat: @"%d", (int)countTotal];
    [[NSUserDefaults standardUserDefaults] setObject:totalVals forKey:@"NumberOfSwamps"];
    //


    NSString * storyboardName = @"Main_iPhone";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"browsePage"];


    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:vc.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Loading Reports";
    [hud show:YES];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         [hud hide:YES];
        if (!error) {
            for (PFObject *obj in objects) {

                NSString *LastName = [obj objectForKey:@"Name"] ;
                NSString *Lat = [obj objectForKey:@"Latitude"] ;
                NSString *Lon = [obj objectForKey:@"Longitude"] ;


                // add values to array
                [Names addObject:LastName];
                [Latitude addObject:Lat];
                [Longitude addObject:Lon];

                [self getResults];

            }

        }

 else{
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }

    }];
}

尝试另一种方式......但这也不起作用......即使HUD是从原始类运行的。

browse.m,

    if (myInt == 8){

            NSLog(@"\n \n Pressed the refresh icon for reports");
            [self removeAllAnnotations];


[standardUserDefaults setInteger:1 forKey:@"HUD_Switch"];   // to make sure switching on
        [standardUserDefaults synchronize];

            [self HUDSwitcher];


            Reports * obje2 = [[Reports  alloc]init];
            [obje2 startGetting];
        }

-(void) HUDSwitcher{

    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger myInt = [standardUserDefaults integerForKey:@"HUD_Switch"];   // 0 for switched off     .... 1 for switched on

    NSString * storyboardName = @"Main_iPhone";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"browsePage"];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:vc.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Testing";
    [hud show:YES];


    if (myInt==0) {
        [hud hide:YES];
    }

}

reports.m

     [standardUserDefaults setInteger:1 forKey:@"HUD_Switch"];   // to switch on the HUD value

    browsePage * obje2 = [[browsePage  alloc]init];
    [obje2 HUDSwitcher];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         [standardUserDefaults setInteger:0 forKey:@"HUD_Switch"];


[standardUserDefaults synchronize];
         browsePage * obje2 = [[browsePage  alloc]init];

        [obje2 HUDSwitcher];
        if (!error) {
            for (PFObject *obj in objects) {

                NSString *LastName = [obj objectForKey:@"Name"] ;
                NSString *Lat = [obj objectForKey:@"Latitude"] ;
                NSString *Lon = [obj objectForKey:@"Longitude"] ;


                // add values to array
                [Names addObject:LastName];
                [Latitude addObject:Lat];
                [Longitude addObject:Lon];

                [self getResults];

            }

        }

 else{
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }

    }];
}

1 个答案:

答案 0 :(得分:0)

当您实例化新的UIViewController vc时,它不在当前视图堆栈中,因此如果您在其视图上显示progressHUD,它将不会出现在屏幕上。

您应该从初始viewController的视图中显示progressHUD。

解决这个问题的一种方法是使用这样的委托:

- (IBAction)testWeather:(id)sender {
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Loading Reports";
    [hud show:YES];

    Reports * obje2 = [[Reports  alloc]init];
    [obje2 startGettingWithDelegate:self];
}

- (void)reports:(Reports*)report didFinishGetting
{
    [self.hud hide:YES];
}

在.h文件的yourViewController界面中添加:

@property (nonatomic, strong) MBProgressHUD *hud;

在报告类

-(void) startGettingWithDelegate:id<yourProtocol> delegate {

    Names= [[NSMutableArray alloc] init];
    Latitude= [[NSMutableArray alloc] init];
    Longitude= [[NSMutableArray alloc] init];


    [Names removeAllObjects];
    [Latitude removeAllObjects];
    [Longitude removeAllObjects];


    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *CountyName = [standardUserDefaults stringForKey:@"CurrentlySelectedCounty"];

    PFQuery *query = [PFQuery queryWithClassName:@"Reports"];
    [query whereKey:@"County" equalTo:@"Universal"];

    //
    NSInteger countTotal= [query countObjects];
    NSString *totalVals = [NSString stringWithFormat: @"%d", (int)countTotal];
    [[NSUserDefaults standardUserDefaults] setObject:totalVals forKey:@"NumberOfSwamps"];
    //

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        // Inform Delegate that finished loading
        [delegate reports:self didFinishLoading];

        if (!error) {
            for (PFObject *obj in objects) {
                NSString *LastName = [obj objectForKey:@"Name"] ;
                NSString *Lat = [obj objectForKey:@"Latitude"] ;
                NSString *Lon = [obj objectForKey:@"Longitude"] ;


                // add values to array
                [Names addObject:LastName];
                [Latitude addObject:Lat];
                [Longitude addObject:Lon];

                [self getResults];
            }
        } else {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

要做到这一点,你应该在Reports.h中创建一个@protocol并使viewController符合它。

在Reports.h

@protocol ReportsProtocol
    - (void)reports:(Reports*) finishedLoading;
@end

@interface ...
.
.
.

在yourViewController.h中

@interface YourViewController : UIViewController <ReportsProtocol>
.
.
.    

另一种解决方案可以是使用块。它有点复杂但更清洁。