2

I want to create a float window above the main window, so code like blow. I use a fake singleton pattern to manage a UIWindow. Because I don't want other object retain the window. The code:

@class testWindow;
static testWindow *singletonSelf;
static dispatch_once_t once;

@implementation testWindow
+ (testWindow *)sharedWindow {
    dispatch_once(&(once), ^{
        singletonSelf = [[testWindow alloc] initWithFrame:CGRectMake(200,200,200,200)];
    });
    return singletonSelf;
}

+ (void)dismiss {
    singletonSelf = nil;
    once = 0;
}
...

- (void)closeButtonClicked {
    self.hidden = YES;
    [testWindow dismiss];
}

There is a button on the window. The method '-closeButtonClicked' will called when the button clicked. When -closeButtonClicked called, I will set the Window hidden property to YES and set singletonSelf,once to nil for the future to create a new window.

But when I did this, the window did not dealloc immediately. The windows property of the [UIApplication sharedApplication] even contain the window I want to release.

Until I tap the screen(any where),the windows property of the [UIApplication sharedApplication] remove the window,than the window will released.

I want to remove the window from windows immediately when I call the +dismiss method.

THX for help. This is my first question on StackOverFlow~~~ Ignore my poor english~

I know the fake singleton is now suggest, but I this is a good choice on this situation. If u have other choice please tell share with me~

S.Captain
  • 167
  • 1
  • 9
  • @MikeAlter I try in this way, but it didn't work. `- (void)buttonClicked { dispatch_async(dispatch_get_main_queue(), ^{ self.hidden = YES; [testWindow dismiss]; }); }` – S.Captain Jun 03 '16 at 05:54
  • Why doesn't the window go out of memory right away?? (Who is retaining this?) What is it about tapping the screen that causes the window to finally be released? – Andrew Paul Simmons Jul 14 '16 at 00:05

0 Answers0