2014-02-06 105 views
-1

爲什麼myView在init之後是dealloc?初始化後的UIViewController dealloc

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 
+0

什麼保留'MOBubbleView'?爲什麼不使用名稱'MOBubbleViewController'(給定一個「視圖」不是「視圖控制器」)? – trojanfoe

+0

「myView alloc」中的myView是什麼?這可能是不正確的。 – proxi

+0

更新了我的帖子 –

回答

1

喲ü將需要保留,你是從你的調用返回的視圖控制器,這樣做:

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

其中pMyViewController聲明的地方,它不會超出範圍 - 比如一個全局變量現在:

eg myView* pMyViewController;

您已將視圖控制器的視圖部分添加到窗口中,因此保留,但實際控制器部分沒有任何引用,因此它被取消分配。

+0

是的,當我在主控制器上聲明全局變量第二控制器時,所有的工作。但是MOBubbleView * bubble = [[MOBubbleView alloc] init];在我給他們打電話時被保留。我看到很多使用這種方法的例子,但是我不明白 –

+0

如何在主視圖中查看我的「泡泡」,以及何時在任何位置釋放自我。但是當他開始的時候,他正在釋放自己 –