Pregunta

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.

¿Fue útil?

Solución

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];    
 }

Otros consejos

Short answer:-

NO, you cannot add subview inside your class which inherits from NSObject. You can only add subview which inherits from viewController. Because every viewController contains instance of view where NSObject does not.

You can add @properties (week) UIView *view; to your myObject.h and in four view controller after myObject creation you can add myObjectObject.view = self.view; and after you call callHud: it should work. Or you can change your method to accept UIView:

-(void) callHud:(NSString*)text view:(UIView*)view{ ...}

And you can call it from your view controller like:

[myObjectObject callHud:@"Your text" view:self.view

In AppDelegate.h

+ (AppDelegate*)sharedDelegate;

In AppDelegate.m

 + (AppDelegate*)sharedDelegate{

    return (AppDelegate*)[UIApplication sharedApplication].delegate;
}

Then in your NSObject.h class

- (void)startHUD;

Then in your NSObject.m class

-(void)startHUD{

    AppDelegate * app = [AppDelegate sharedDelegate];

    //--- to start the activity indicatior ----
    MBProgressHUD *  hud = [MBProgressHUD showHUDAddedTo:app.window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    //hud.contentColor = [UIColor colorWithRed:0.f green:0.6f blue:0.7f alpha:1.f];
    [app.window bringSubviewToFront:hud];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top