iOS之UI初级---UIWindow的基本内容

本文参考:唐巧的《iOS进阶》

一、UIWindow简介

在 iOS 应用中,我们使用 UIWindow 和 UIView 来呈现界面。UIWindow 并不包含任何默认 的内容,但是它被当作 UIView 的容器,用于放置应用中所有的 UIView。

二、是不是只要是新创建的 UIWindow 一定就会覆盖在界面的最上面呢?

UIWindow 有一个类型为“UIWindowLevel”的属性,该属性定义了 UIWindow 的层级,系 统定义的 WindowLevel 一共有 3 种取值,如下所示:

①UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal;
②UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert;
③UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar;

我们通过如下代码将这些值输出:

NSLog(@”UIWindowLevelNormal=%f␣UIWindowLevelStatusBar=%f␣UIWindowLevelAlert=%f”, UIWindowLevelNormal, UIWindowLevelStatusBar, UIWindowLevelAlert);

最终得到的结果是:(在实际中,WindowLevel 的取值并不限于下面输出的值。)

UIWindowLevelNormal=0.000000
UIWindowLevelStatusBar=1000.000000
UIWindowLevelAlert=2000.000000

三、UIWindow的一个小应用

当程序从后台重新回来,跳出一个密码输入界面。密码输入正确才能继续操作

#import "PasswordWindow.h"

@implementation PasswordWindow {
    UITextField *_textField;
}

+ (PasswordWindow *)sharedInstance
{
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });

    return sharedInstance;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
        label.text = @"请输入密码";
        [self addSubview:label];

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 80, 200, 30)];
        textField.backgroundColor = [UIColor whiteColor];
        textField.secureTextEntry = YES;
        [self addSubview:textField];

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 110, 200, 44)];
        [button setBackgroundColor:[UIColor blackColor]];
        button.titleLabel.textColor = [UIColor blackColor];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(completeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];

        self.backgroundColor = [UIColor yellowColor];
        _textField = textField;
    }
    return self;
}

- (void)completeButtonPressed:(UIButton *)sender
{
    if ([_textField.text isEqualToString:@"abcd"]) {

        //因为我们处理的UIWindow需要处理键盘事件,所以需要把它设置为keyWindow
        [_textField resignFirstResponder];
        [self resignKeyWindow];

        self.hidden = YES;
    }
    else
    {
        [self showErrorAlertView];
    }
}

- (void)showErrorAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"密码错误" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alertView show];
}

- (void)show
{
    [self makeKeyWindow];
    self.hidden = NO;
}

@end
//AppDelegate.m文件
- (void)applicationDidEnterBackground:(UIApplication *)application {

    [[PasswordWindow sharedInstance] show];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值