iOS开发 全局Alert(不依赖ViewController)的两种实现方案详解

本文介绍了在iOS开发中如何实现在任意视图上弹出全局Alert,详细讲解了两种实现方案:一是通过扩展UIWindow,二是通过扩展UIAlertController。并讨论了两种方案的优缺点及使用场景。
摘要由CSDN通过智能技术生成

开发中遇到这样的需求,需要实现在后台接受到某一事件时无论当前用户处在哪个视图都需要弹出一个Alert.

参考了一些网上的资料,加上自己的思考,总结出两种方案:

1)对UIWindow进行扩展

UIWindow+.h

#define UIEventSubtypeMotionShakeNotification @"UIEventSubtypeMotionShakeNotification"


#import <UIKit/UIKit.h>

@interface UIWindow (Motion)

// @override
- (BOOL)canBecomeFirstResponder;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

@end

UIWindow+.m

//
//  UIWindow.m
//  Demo
//
//  Created by simon on 2018/8/18.
//  Copyright © 2018年 simon. All rights reserved.
//

#import "UIWindow+.h"

@implementation UIWindow (Motion)


- (BOOL)canBecomeFirstResponder {    NSLog(@"csm canBecomeFirstResponder");
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值