Search code examples
iosobjective-cmbprogresshud

can we use viewController view in NSObject?


myObject.h

@interface serverBaglantisi : NSObject<MBProgressHUDDelegate> {
    MBProgressHUD *HUD;
}

myObject.m

-(void) callHud:(NSString*)text{

    HUD = [[MBProgressHUD alloc] initWithView:self.view]; // here 

    [self.view addSubview:HUD];// here
    HUD.delegate = self;
    HUD.labelText = text;
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
}

I am also use MBProgressHud class. If i add "callHud" method in viewController.m(and also add myProgressTask to in viewController.m) then everything works. But I wonder if it is possible to call inside my NSObject successfully?

Sorry for noob question i am new at iOS developer.


Solution

  • Np, you can't. NSObject does not a define a property called view. But you can pass the view controller to the object:

     - (void)showHudWithMessage:(NSString*)text fromViewController:(UIViewController *)viewController
     {
          HUD = [[MBProgressHUD alloc] initWithView:viewController.view]; 
          [viewController.view addSubview:HUD];
          HUD.delegate = self;
          HUD.labelText = text;
          [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];    
     }