IOS development UI-program startup principle and UIApplication
I. UIApplication
1. Brief Introduction
(1) A UIApplication object represents an application. A UIApplication object represents an application.
(2) Each application has its own UIApplication object, which is a singleton. If you try to create a new UIApplication object in the program, an error is reported.
(3) The singleton object can be obtained through [UIApplicationsharedApplication ].
(4) The first object created after an iOS program is started is the UIApplication object, and there is only one (get two UIApplication objects through code, and print the address to see that the address is the same ).
(5) use the UIApplication object to perform some application-level operations
2. Application-level operation example:
1) set the red alarm number in the upper-right corner of the application icon (for example, when a QQ message is sent, the icon displays 1, 2, and 3 new messages .)
@ Property (nonatomic) NSInteger applicationIconBadgeNumber;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Create and add a button
UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (100,100, 60, 30)];
[Btn setTitle: @ "button" forState: UIControlStateNormal];
[Btn setBackgroundColor: [UIColor brownColor];
[Btn addTarget: self action: @ selector (onClick) forControlEvents: UIControlEventTouchUpInside];
[Self. view addSubview: btn];
}
-(Void) onClick
{
NSLog (@ "Click Event ");
// Error. Only one unique UIApplication object can be created.
// UIApplication * app = [[UIApplication alloc] init];
// Obtain the UIApplication object of the program through sharedApplication
UIApplication * app = [UIApplication sharedApplication];
App. applicationIconBadgeNumber = 123;
}
2) set the visibility of the network indicator
@ Property (nonatomic, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
Code and effects:
// Sets the online animation of the indicator.
App. networkActivityIndicatorVisible = YES;
3) Manage the status bar
From iOS 7, the system provides two methods to manage the status bar.
A. Manage through UIViewController (each UIViewController can have its own different status bar ).
In iOS7, the status bar is managed by UIViewController by default. The following methods can be implemented by UIViewController to easily manage the visibility and style of the status bar.
Style of the status bar-(UIStatusBarStyle) preferredStatusBarStyle;
Status Bar visibility-(BOOL) prefersStatusBarHidden;
# Pragma mark-set the style of the status bar
-(UIStatusBarStyle) preferredStatusBarStyle
{
// Set it to white
// Return UIStatusBarStyleLightContent;
// Black by default
Return UIStatusBarStyleDefault;
}
# Pragma mark-set whether the status bar is hidden (NO)
-(BOOL) prefersStatusBarHidden
{
Return NO;
}