iOS Basics-Custom views, view controllers

Source: Internet
Author: User

Custom View

For example, on the landing page, on the left label, on the right textfield. Custom

@interface Ltview:uiview@property (nonatomic,retain) UILabel *label; @property (nonatomic,retain) Uitextfield * textfield;-(Instancetype) initWithFrame: (CGRect) frame text: (NSString *) text placeHolder: (NSString *) placeholder;@ End


@implementation Ltview
-(Instancetype) initWithFrame: (CGRect) frame{
self = [super Initwithframe:frame];
if (self) {
Definition width and height
CGFloat w = frame.size.width;
CGFloat h = frame.size.height;
Self.label = [[UILabel alloc]initwithframe:cgrectmake (5, 5, W/3-h-10)];
[Self AddSubview:self.label];

Self.textfield = [[Uitextfield alloc]initwithframe:cgrectmake (W/3-5, 5, 3*W/3, h-10)];
Self.textField.layer.cornerRadius = 8;
Self.textField.layer.borderWidth = 1;
[Self AddSubview:self.textField];

[Self.label release];
[Self.textfield release];

}return self;
}

-(void) dealloc{
[Self.label release];
[Self.textfield release];

[Super Dealloc];
}



-(Instancetype) initWithFrame: (CGRect) frame text: (NSString *) text placeHolder: (NSString *) placeholder{
self = [self initwithframe:frame];
if (self) {
Self.label.text = text;
Self.textField.placeholder = placeholder;
}
return self;

}

View Controller

Overview Uiviewcontroller: View Controller

control view display, response time

Share the work of appdelegate

Independence of modules for improved reusability

Function

control view size changes, layout view, response events

Detecting and handling memory warnings

Detecting and handling screen rotations

Toggle the detection View

MVC Overview

Uiviewcontroller is the core of MVC design pattern

MVC is a framework-level design pattern

M is model, mainly used to build a data model

V is view,view main function is to display the data

C is the controller, mainly the communication of the control M and V

Uiviewcontroller comes with an empty view, does not meet the requirements,

The view controller is only responsible for controlling the view display, responding to events

So use the custom view class:

1. Custom view inheritance UIView, adding a child view control in the initialization method

2. Rewrite the controller's Loadview method, create a custom attempt object, and specify the view for the controller

3. Set the Child view Control object as a property of the custom view class in the Viewdidload method

@implementation viewcontroller-(Instancetype) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self    = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];    if (self) {        NSLog (@ "1");    } return self;} -(void) loadview{    [Super Loadview];    Rewrite    UIView *view = [[UIView alloc]initwithframe:[uiscreen mainscreen].bounds];    View.backgroundcolor = [Uicolor browncolor];    Self.view = view;    NSLog (@ "2");} -(void) Viewwilldisappear: (BOOL) animated{    [Super Viewwilldisappear:yes];    NSLog (@ "3");} -(void) Viewdidappear: (BOOL) animated{    [Super Viewdidappear:yes];    NSLog (@ "4");}

View controller function One,//1. Detection and response time//2. The overall layout of the view//3. Detect screen rotation//4. Detect and handle memory warnings//1. Whether the screen can be rotated (default rotation)-(BOOL) shouldautorotate{    return YES ;} The direction the screen can rotate-(nsuinteger) supportedinterfaceorientations{    return uiinterfaceorientationmaskallbutupsidedown;}

Memory warning-(void) didreceivememorywarning {    [super didreceivememorywarning];    If the view is loaded and the view is not displayed, release it if    ([self isviewloaded] = = YES && Self.view.window = nil) {        Self.view = nil ;    }    Dispose of any resources the can be recreated.}

iOS Basics-Custom views, view controllers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.