iOS開(kāi)發(fā)之UIWindow的使用

一、UIWindow簡(jiǎn)介

UIWindow是最頂級(jí)的界面容器。
UIWindow繼承自UIView。

UIWindow的主要功能:

  1. 作為UIView的最頂級(jí)容器,包含應(yīng)用顯示所需要的所有的UIView。
  2. 傳遞觸摸消息和鍵盤事件給UIView。

UIWindow添加UIView有兩種方式:

  1. 通過(guò)調(diào)用addSubView方法。
  2. 通過(guò)設(shè)置其特有的rootViewController屬性。通過(guò)設(shè)置該屬性為要添加view對(duì)應(yīng)的UIViewController,UIWindow將自動(dòng)將其view添加到當(dāng)前window中,同時(shí)負(fù)責(zé)ViewController和view的生命周期。

二、UIWindow的使用

UIWindow通過(guò)UIWindowLevel設(shè)置window的層級(jí)。
例創(chuàng)建UIWindow的子類,實(shí)現(xiàn)調(diào)出輸入密碼界面。

#import <UIKit/UIKit.h>

@interface PasswordInputWindow : UIWindow
+ (PasswordInputWindow *)sharedInstance;
- (void)show;
@end

#import "PasswordInputWindow.h"

@implementation PasswordInputWindow{
    UITextField *_textField;
}

+(PasswordInputWindow *)sharedInstance{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc]initWithFrame:[UIScreen mainScreen].bounds];
    });
    return sharedInstance;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 20)];
        label.text = @"請(qǐng)輸入密碼";
        [self addSubview:label];
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 80, 200, 20)];
        textField.backgroundColor = [UIColor yellowColor];
        textField.secureTextEntry = YES;
        [self addSubview:textField];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 110, 200, 44)];
        [button setBackgroundColor:[UIColor redColor]];
        button.titleLabel.textColor = [UIColor whiteColor];
        [button setTitle:@"登錄" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        self.backgroundColor = [UIColor yellowColor];
        _textField = textField;
    }
    return self;
}
- (void)show{
    [self makeKeyWindow];
    self.hidden = NO;
}
- (void)completeButtonPressed:(id)sender{
    if ([_textField.text isEqualToString:@"1234"]) {
        //正確
        [_textField resignFirstResponder];
        [self resignKeyWindow];
        self.hidden = YES;
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密碼輸入錯(cuò)誤" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    

    }
}

//后臺(tái)進(jìn)入時(shí)調(diào)用

    [[PasswordInputWindow sharedInstance] show];

一般,手勢(shì)解鎖頁(yè)、啟動(dòng)介紹頁(yè)、應(yīng)用內(nèi)的通知提醒、彈窗廣告等適合用UIWindow來(lái)實(shí)現(xiàn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,738評(píng)論 1 14
  • 7、不使用IB是,下面這樣做有什么問(wèn)題? 6、請(qǐng)說(shuō)說(shuō)Layer和View的關(guān)系,以及你是如何使用它們的。 1.首先...
    AlanGe閱讀 991評(píng)論 0 1
  • 二、使用UIWindow 1、簡(jiǎn)介在iOS App中,UIWindow是最頂層的界面內(nèi)容,我們使用UIWindow...
    南華coder閱讀 2,848評(píng)論 0 9
  • 前言 《鍛冶屋英雄譚》目前限免,所以喜歡的朋友可以踴躍下載。 以前玩家是英雄,現(xiàn)在是幕后操縱者 “英雄譚”三個(gè)字會(huì)...
    悶瓜愛(ài)游戲閱讀 2,594評(píng)論 0 9
  • 1. 仿寫(xiě)汪增祺《八千歲》中的一段話,表達(dá)自己的某一種濃烈的情感。 每當(dāng)我回家,總會(huì)第一時(shí)間跑到那孤冷的花園...
    林爾姑娘閱讀 323評(píng)論 1 0

友情鏈接更多精彩內(nèi)容