我们可以在NSObject中使用viewController视图吗?

时间:2013-12-10 10:23:59

标签: ios objective-c mbprogresshud

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

我也使用MBProgressHud类。如果我在viewController.m中添加“callHud”方法(并在viewController.m中添加myProgressTask),那么一切正常。但我想知道是否有可能成功调用我的NSObject?

对不起,我是iOS开发人员的新手。

4 个答案:

答案 0 :(得分:0)

简短回答: -

不,你不能在你的类中添加继承自NSObject的子视图。您只能添加从viewController继承的子视图。因为每个viewController都包含NSObject没有的视图实例。

答案 1 :(得分:0)

Np,你不能。 NSObject没有定义名为view的属性。但是您可以将视图控制器传递给对象:

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

答案 2 :(得分:0)

你可以在创建myObject之后将@properties (week) UIView *view;添加到myObject.h和四个视图控制器中,你可以添加myObjectObject.view = self.view;在你打电话给callHud之后:它应该有效。或者你可以改变你的方法来接受UIView:

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

您可以从视图控制器中调用它,如:

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

答案 3 :(得分:0)

在AppDelegate.h中

+ (AppDelegate*)sharedDelegate;

在AppDelegate.m中

 + (AppDelegate*)sharedDelegate{

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

然后在你的NSObject.h类中

- (void)startHUD;

然后在你的NSObject.m类

-(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];
}
相关问题