UIWindow introduction and uiwindow Introduction
During normal development, we may ignore UIWindow. Because the system has helped us deal with its operations. For example, when the program is started. Call the makeKeyAndVisible method to make the entire program interface visible.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = [self rootViewController]; [self.window makeKeyAndVisible]; return YES;}
However, the commonly used project creation should be "Single View Application". Apple helped us encapsulate it more thoroughly. The above Code cannot be seen. This is not good for us to understand the underlying implementation process.
In the IOS app, we use UIWindow and UIView to present the interface. UIWindow does not contain any default content, but it is used as a container for UIView and is used to place all the uiviews in the application. From the inheritance relationship, UIWindow inherits from UIView. Therefore, in addition to all functions of UIView, UIWIndow also adds some special attributes and methods.
The main function of UIWindow: 1. As the top-level container of UIView, it contains all the uiviews required by the application. 2. Send the touch message and keyboard event to UIView (Note: If you have any questions about event message transmission, please refer to my previous blog. Click here ).
The usage of UIWindow by the system. Usually there is only one UIWindow in a program, but sometimes when we call the system controls (such as UIAlertView), the IOS system ensures that UIAlertView is on all interfaces, it will create a new UIWindow temporarily. By setting the UIWindowLevel of its UIWindow to a higher level, the UIWindow will be built on all application interfaces (friends familiar with html should know that, the "mask effect" on the Internet is to control the upper and lower relations of layers by setting the z-index attribute of the element. It should be a truth ).
Sometimes, we also want to overwrite some interfaces in application development to the top of all interfaces. At this time, we can manually create a new UIWindow. For example, if you want to implement a password protection function, you can press the Home key on any page of the application and switch back from the background in a short time, a password input interface is displayed.
The Demo interface is very simple. Every time you start an application or enter the application from the background, the password input interface is displayed. The application can be used only when the password is entered correctly.
The rough code is as follows: PasswordInputWindow. h file. Define a subclass PasswordInputWindow that inherits from UIWindow. The show method is used to display the password input interface.
@interface PasswordInputView : UIWindow+ (PasswordInputView *)shareInstance;- (void)show;@end
PasswordInputWindow. m file.
# Import "PasswordInputView. h "@ interface PasswordInputView () @ property (nonatomic, weak) UITextField * textField; @ end @ implementation PasswordInputView # pragma mark-Singleton + (PasswordInputView *) specified instance {static id instance = nil; static dispatch_once_t onceToken; dispatch_once (& onceToken, ^ {instance = [[self alloc] initWithFrame: [UIScreen mainScreen]. bounds] ;}); return instance ;}# pragma mark-Initilize-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self setup];} return self;}-(instancetype) initWithCoder :( NSCoder *) decoder {if (self = [super initWithCoder: decoder]) {[self setup];} return self;}-(void) setup {UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (10, 50,200, 20)]; label. text = @ "enter the password"; [self addSubview: label]; UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake (10, 80,200, 20)]; textField. backgroundColor = [UIColor whiteColor]; textField. secureTextEntry = YES; [self addSubview: textField]; self. textField = textField; UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (10,110,200, 44)]; [button setBackgroundColor: [UIColor blueColor]; [button setTitle: @ "OK" forState: UIControlStateNormal]; [button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [button addTarget: self action: @ selector (completeButtonPressed :) forControlEvents: Success]; [self addSubview: button]; self. backgroundColor = [UIColor yellowColor] ;}# pragma mark-Common Methods-(void) completeButtonPressed :( UIButton *) button {if ([self. textField. text isEqualToString: @ "123456"]) {[self. textField resignFirstResponder]; [self resignKeyWindow]; self. hidden = YES;} else {[self showErrorAlertView] ;}}- (void) Comment {UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: nil message: @ "Incorrect password, enter "delegate: nil cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alertView show];}-(void) show {[self makeKeyWindow]; self. hidden = NO;} @ end
1. In the code, I implemented the initWithFrame and initWithCoder methods to ensure that no matter whether the user is pure code or the initialization of the xib implementation, there is no problem. 2. If the created UIWindow needs to handle Keyboard Events, set it to keyWindow reasonably. KeyWindow is a UIWindow designed by the system to accept keyboard and other non-touch events. We can use the makeKeyWindow and resignKeyWindow methods to set the self-created UIWindow instance to keyWindow. 3. Add the following code to ensure that the password input interface is displayed every time the program enters from the background.
- (void)applicationDidBecomeActive:(UIApplication *)application { [[PasswordInputView shareInstance] show];}