Search code examples
iosobjective-cuiviewcontrolleruiwindow

Custom UIViewController add issue


I Created a UIViewController that I want to add on the top of all of my views, the problem is that it's visible for only 1 sec and then disappears from the screen and deallocated. Why this is happening?

This is my code:

- (void)show
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.windowLevel = UIWindowLevelAlert;
    self.window.screen = [UIScreen mainScreen];
    [self.window makeKeyAndVisible];

    UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
    [mainWindow setUserInteractionEnabled:NO];
    [self.window addSubview:self.view];

    self.view.alpha = 0.0f;
    self.backgroundImage.image = [self takeSnapshot];

    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        self.view.transform = CGAffineTransformIdentity;
        self.view.alpha = 1.0f;

    } completion:nil];
}

- (void)dealloc
{
    NSLog(@"Screen deallocated.");
}

Solution

  • Looking at the graph of your objects,I assume self is some view controller. your self.view is subview of Appdelegates UIWindow. When you do

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    

    your Appdelegate window gets released subsequently your view controller(self) will also be released.

    So I would suggest you to retain UIWindow instance before actually releasing it.