-1

Why myView is dealloc after init?

MainViewController:

[MOBubbleView hudWithBody:@"123123" bubblePoint:CGPointMake(220, headerMenu.center.y) hidesAfter:2 show:YES];

MOBubbleView.h:

@interface MOBubbleView : UIViewController

@property (nonatomic, assign) float hudHideDelay;
@property (nonatomic, strong) UIColor *itemColor;

+ (MOBubbleView*)hudWithBody:(NSString*)body bubblePoint:(CGPoint)rect hidesAfter:(float)delay show:(BOOL)show;

@end

MOBubbleView.m:

+ (MOBubbleView*)hudWithBody:(NSString*)body bubblePoint:(CGPoint)point hidesAfter:(float)delay show:(BOOL)show {

    MOBubbleView *bubble = [[MOBubbleView alloc] init];

///....

    if (show) [bubble addToWindow];

    return bubble;
}

- (void)addToWindow {
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.view];
}

- (void)loadView {
    CGRect bounds = [[UIScreen mainScreen] bounds];
    self.view = [[UIView alloc] initWithFrame:bounds];
    [self.view setBackgroundColor:[UIColor clearColor]];

   /// .. my animation


}

- (void) dealloc {
    NSLog(@"Close myView");
}

@end
dev.nikolaz
  • 3,162
  • 3
  • 16
  • 29

1 Answers1

1

You would need to retain the view controller that you are returning from your call, so do:

pMyViewController = [myView hudWithBody:@"123123" bubblePoint:CGPointMake(220, headerMenu.center.y) hidesAfter:2 show:YES];

where pMyViewController is declared somewhere that it won't go out of scope - say a global variable for now:

e.g. myView* pMyViewController;

You have added the view part of the view controller onto the window, so that's retained, but the actual controller part has no references holding on to it, so it gets deallocated.

NullDev
  • 13,073
  • 4
  • 44
  • 50
  • Yes, when I declared global variable second controller on main controller, all work. But MOBubbleView *bubble = [[MOBubbleView alloc] init]; is retained when I called them. I saw many examples where used this method, but I can't understand how – dev.nikolaz Feb 06 '14 at 16:45
  • I want to view my "bubble" in main view and when I tap anywhere deallocated self. But he is dealloc himself when start – dev.nikolaz Feb 06 '14 at 16:49