用戶從任一界面按Home鍵退出,再從後臺切換回來時,顯示一個密碼輸入界面,只有用戶輸入正確的密碼,才能進入退出前的界面。
需求分析
由於密碼輸入界面可能從任何應用界面彈出,且需要蓋在所有界面的最上層,所以需喲用UIView來實現
基礎知識
UIWindow的主要作用:
1.作爲UIView的最頂層容器,包含應用顯示需要的所有的UIView
2. 傳遞觸摸消息和鍵盤事件給UIView
具體實現
+ (PasswordInputWindow *)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) {
//setup UI
}
- (void)show
{
[self makeKeyWindow];
self.hidden = NO;
}
在APPDelegate的下面回調方法中使用該Window
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[PasswordInputWindow sharedInstance] show];
}
額外的獎勵
通過創建UIWindow,我們可以很容易的實現將某些特定界面置於最上層的效果,但是這種特性最好只用於無法確定該視圖應該在哪個控制器上面,若明確視圖應該出現在哪個控制器上面,則一般選擇在該控制器上創建新的視圖來實現該功能。此外,在一個項目中不應該過多的創建這種UIWindow單例對象,衆所周知,單例會一直保持在內存中,得不到釋放!
搬磚工看這裏: http://download.csdn.net/detail/luozhonglan/8608207
******************************************************************************************************************************************************************************************************
posted by 羅大佑子
2015-4-18