Created by Guo Chai April 14, 2015 21:34:01
A. Custom view:
Depending on the requirements, the classes that the custom view inherits are also different. General? Custom
The view is inherited from the UIView. The following are the key points of the custom view:
1. Create a UIView? subclass
2, in the class initialization? method to add the child view
3. The. h file of the class is provided? Some access to the mouth (? method), easy to operate outside? Child view
==================
Here is an example of a Label-textfield custom view:
By encapsulating the label and TextField in the Ltview, we have simplified our code to a certain extent.
Often we need to ltview in the label or TextField into the line? A certain control, such as: Set the text of the label, get the text of TextField, give TextField specify delegate, set TextColor and so on.
To make it easier for the outside world to manipulate labels and textfield, so we have to provide some methods for the outside world.
Code implementation:
@interface Ltview:uiview@property (nonatomic,retain) UILabel * lable; @property (nonatomic,retain) Uitextfield * textfield;-(Instancetype) initWithFrame: (cgrect) frame andlabeltext: (NSString *) LabelText Andplaceholder :(NSString *) placeholder; @end
Refers to defining the view, overriding init-(ID) init{self = [super init]; if (self) {//self = [self initwithframe:cgrectzero]; } return self;} -(ID) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {UILabel * lable = [[UILabel alloc]i Nitwithframe:cgrectmake (0, 0, FRAME.SIZE.WIDTH/3, frame.size.height)]; Lable.backgroundcolor = [Uicolor Orangecolor]; [Self addsubview:lable]; The created lable is assigned a value of _lable self.lable = lable; [lable release]; ========================= Uitextfield * TextField = [[Uitextfield alloc]initwithframe:cgrectmake (Frame.size.widt H/3, 0, FRAME.SIZE.WIDTH*2/3, frame.size.height)]; Textfield.backgroundcolor = [Uicolor Yellowcolor]; Self.textfield = TextField; [Self Addsubview:textfield]; [TextField release]; } return self;} -(void) dealloc{[_lable release]; [_textfield release]; [Super Dealloc];} -(Instancetype) initWithFrame: (CGRect) Frame AndlAbeltext: (NSString *) LabelText Andplaceholder: (NSString *) placeholder{self = [self initwithframe:frame]; if (self) {self.lable.text = LabelText; Self.lable.font = [Uifont systemfontofsize:15]; Self.textField.placeholder = placeholder; } return self;} -(void) Setframe: (CGRect) frame{[Super Setframe:frame]; Self.lable.frame = CGRectMake (0, 0, FRAME.SIZE.WIDTH/3, frame.size.height); Self.textField.frame = CGRectMake (FRAME.SIZE.WIDTH/3, 0, FRAME.SIZE.WIDTH*2/3, frame.size.height);}
The LoginView view calls the custom view:
CGRect newframe = CGRectMake (0, 0, 320, 480); self = [super Initwithframe:newframe]; if (self) {Ltview * ltview = [[[Ltview Alloc]initwithframe:cgrectmake] andlabeltext:@ "username" Andplace holder:@ "Please enter user name"]; [Self addsubview:ltview]; Delegate ltView.textField.delegate = self; Ltview.tag = 100; [Ltview release]; ============ Ltview * ltView2 = [[Ltview alloc]initwithframe:cgrectmake (a) andlabeltext:@ "password" and placeholder:@ "Please enter your password"]; LtView2.textField.secureTextEntry = YES; LtView2.textField.delegate = self; [Self addsubview:ltview2]; Ltview2.tag = 200; [LtView2 release]; ============ UIButton * loginbtn = [UIButton buttonwithtype:uibuttontypecustom]; Loginbtn.frame = CGRectMake (50, 230, 60, 50); Loginbtn.backgroundcolor = [Uicolor Redcolor]; [Loginbtn settitle:@ "Landing" forstate:uicontrolstatenormal]; [LOGINBTN addtarget:self Action: @selector (Login:) forcontrolevents:uicontroleventtouchupinside]; [Self addsubview:loginbtn]; ===================== UIButton * loginBtn2 = [UIButton buttonwithtype:uibuttontypecustom]; Loginbtn2.frame = CGRectMake (130, 230, 60, 50); Loginbtn2.backgroundcolor = [Uicolor Redcolor]; [LOGINBTN2 settitle:@ "registration" forstate:uicontrolstatenormal]; [Self addsubview:loginbtn2]; ============210 UIButton * loginBtn3 = [UIButton buttonwithtype:uibuttontypecustom]; Loginbtn3.frame = CGRectMake (210, 230, 100, 50); Loginbtn3.backgroundcolor = [Uicolor Redcolor]; [LoginBtn3 settitle:@ "Retrieve password" forstate:uicontrolstatenormal]; [LoginBtn3 addtarget:self Action: @selector (SEARCHPSW:) forcontrolevents:uicontroleventtouchupinside]; [Self addsubview:loginbtn3];
Two. View Controller:
The custom view class inherits UIView. Add the child view control in the Initialize? method.
Rewrite the controller's Loadview? method. Create a custom View object and specify it as the controller's view.
Set the???????????????????????????: Add action, set delegate, and so on.
Add a button click event Implementation and Proxy in the Controller? Implementation of the method
-(void) viewdidload{ [Super Viewdidload]; Do any additional setup after loading the view.// self.view.backgroundColor = [Uicolor greencolor];// Uiswitch *aswitch = [[Uiswitch alloc]initwithframe:cgrectmake (]];// [Self.view addsubview:aswitch];// [Aswitch release]; =============================== //LoginView to cover the Thirdviewcontroller * THIRDVC = [[Thirdviewcontroller Alloc]init]; [Self.view AddSubview:thirdVC.view]; Retain [self ADDCHILDVIEWCONTROLLER:THIRDVC]; [THIRDVC release]; } ========-(void) loadview{ LoginView * LOGV = [[LoginView alloc]initwithframe:cgrectmake (0, 0, +, 480)]; Self.view = LOGV; [LOGV release]; }
Third, detect screen rotation:
Note that the view controller will automatically adjust the view's size to fit the screen rotation, bounds
Modified to trigger the view's layoutsubviews? method.
View Rewrite Layoutsubviews? method, re-layout according to device direction.
[UIApplication Shareapplication].statusbarorientation provides the device's current direction.
Controller:
-(Nsuinteger) supportedinterfaceorientations{ //Set when the screen rotates, whether the font follows rotation, according to the bitwise OR operation //inside the value of the direction of 1,2,4,8 .... For example 3 = 1 + 2 means two directions return Uiinterfaceorientationmasklandscapeleft | uiinterfaceorientationmaskportrait;}
In view:
Rewrite//When the view needs to be redrawn, the Layoutsubviews method of the view is executed-(void) layoutsubviews{ //Gets the direction of the device if ([uiapplication Sharedapplication].statusbarorientation = = Uiinterfaceorientationlandscapeleft | | [UIApplication sharedapplication].statusbarorientation = = uiinterfaceorientationlandscaperight) { UIView * V1 = [self viewwithtag:100]; Here you need to rewrite the Setframe method v1.frame = CGRectMake ( A. V1.backgroundcolor = [Uicolor redcolor]; } else { UIView * v1 = [self viewwithtag:100]; V1.frame = CGRectMake (a); V1.backgroundcolor = [Uicolor redcolor]; }}
Four, handling memory warnings:
The controller can monitor memory warnings so that we avoid crash caused by insufficient memory. Override the Didreceivememorywarning method in a defined controller? subclass. Release resources that are not used temporarily. (data object, image)
Controller's Method:
-(void) didreceivememorywarning{ [Super didreceivememorywarning]; Dispose of any resources the can be recreated. When the view has been loaded into memory and is not displayed on the window, empty the view if ([self isviewloaded] && Self.view.window = = nil) { Self.view = nil ; }}
UI Development----Custom views and view controllers (Controller)