문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top