LoginSignup
31
30

More than 5 years have passed since last update.

独自UIWindowの生成と解放

Last updated at Posted at 2013-12-16

独自にUIWindowのサブクラスを定義して使う方法について。ARC前提。

ウインドウ生成

まずウインドウのインスタンスを作る。サブクラス化するとカスタマイズしやすい。ウインドウレベルを設定すると、ステータスバーやアラートの上に表示する事も可能。
Cocoa Controls 界隈で見かける「ステータスバーの上に表示する系」のものはウインドウレベルを利用していることが多い。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // ウインドウレベルを設定したり
        self.windowLevel = UIWindowLevelAlert;
    }
    return self;
}

※サブクラス化は必須ではありません。

ウインドウを表示。

self.myWindow = [[MyWindow alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
[self.myWindow makeKeyAndVisible]; // これでウインドウを表示

この時点で、[UIApplication sharedApplication].windows に myWindow のインスタンスが含まれるようになる。

ウインドウ解放

UIWindow には -resignKeyWindow メソッドが存在するが、これは直接実行してはだめなものらしい。

resignKeyWindow
Invoked automatically when the window resigns key window status; never invoke this method directly.

self.myWindow を nil にするとウインドウへの参照が無くなるので、ウインドウを破棄する事が出来る。

self.myWindow = nil;

次の RunLoop で [UIApplication sharedApplication].windows を見ると myWindow が破棄されている事が確認出来る。

キーウインドウと UIScrollView

テーブルビュー等のスクロールビューがある画面でステータスバーをタップすると最上位まで自動スクロールしてくれる。どうやらこれはキーウインドウ内のビューに通知されるもののようで、独自ウインドウがキーウインドウになっていると元のウインドウのビューには通知されなくなる。

元のウインドウをキーウインドウに戻すには、-makeKeyWindow -makeKeyAndVisible のいずれかのメソッドを実行する。

31
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
30