iOS Development Basics-Fragmentation 35

Source: Internet
Author: User
Tags uikit

1:ios View Control object life cycle

init-Initialization Program

viewdidload-Load View

Called when the view of the Viewwillappear-uiviewcontroller object is about to join the window;

Called when the view of the Viewdidapper-uiviewcontroller object has been added to the window;

The view of the Viewwilldisappear-uiviewcontroller object is about to disappear, be overwritten, or be hidden when called;

The Viewdiddisappear-uiviewcontroller object's view has disappeared, is overwritten, or is hidden when called;

Execution Time Order

15:51:44.811INHYRON[483:B903] Init

15:51:54.081INHYRON[483:B903] Viewdidload

15:51:54.082INHYRON[483:B903] Viewvillappear

15:51:54.084 inhyron[483:b903] Viewdidappear

Obviously, Init is executed first, then Viewdidload is executed, then Viewwillappear is finally viewdidappear, so the view is created, when the view disappears or is overwritten:

15:54:14.557INHYRON[483:B903] Viewwilldisappear

15:54:14.558INHYRON[483:B903] Viewdiddisappear

So the view disappears.

2: Initializes an instance class with a default value

userManager.h file contents: #import<Foundation/Foundation.h>@interface Usermanager:nsobject@property (nonatomic,copy) NSString*UserName, @property (nonatomic,copy) NSString*PassWord; @property (assign, nonatomic) Nsinteger type;+ (Usermanager *) Usermanagerwithtype: (Nsinteger) type;-(NSString *) Togetparams;-(void) Configwithobj: (Usermanager *) Usermanagerinfo; @enduserManager. m file contents: #import"UserManager.h"@implementation Usermanager-(instancetype) init{ Self=[Super Init]; if(self) {_username=@"Root"; _password=@"123456"; _type=0; }    returnSelf ;}+ (Usermanager *) Usermanagerwithtype: (nsinteger) type{Usermanager*mymanager=[[Usermanager alloc]init]; Mymanager.type=type; returnMymanager;}-(void) Configwithobj: (Usermanager *) usermanagerinfo{Self.username=Usermanagerinfo.username; Self.password=Usermanagerinfo.password; Self.type=Usermanagerinfo.type;}-(NSString *) togetparams{return[NSString stringWithFormat:@"Current User:%@ password:%@", Self.username,self.password];} @end calling code: Usermanager*mymanager=[usermanager Usermanagerwithtype:4]; NSLog (@"User information:%@", mymanager.togetparams);

3:nshttpcookiestorage (Get and delete cookies)

*cookiesarray =*cookiedict =*cookie = [cookiedict objectforkey:@ "cookie" ]; // set the HTTP header's Cookie [URLRequest Setvalue:cookie Forhttpheaderfield:@ "cookie"]; Delete Cookiensarray *cookiesarray = [[ Nshttpcookiestorage Sharedhttpcookiestorage] Cookies];for (Nshttpcookie *cookie in Cookiesarray) {    [[ Nshttpcookiestorage Sharedhttpcookiestorage] Deletecookie:cookie];}

4:ios about implementing different countdown implementations in multiple cells in UITableView

//all remaining time arraysNsmutablearray *totallasttime; In all data requested by the network, the remaining time of the data in which the countdown is to be displayed is saved separately, if this is all the data need to countdown, then only need to save time, if there is some data need countdown, You can save the dictionary with two key-value pairs for their indexpath in UITableView and the remaining time: num defaults from 0 nsdictionary*dic = @{@"Indexpath": [Nsstrin stringWithFormat:@"%i", Num],@"Lasttime": Order.paylasttime}; [Totallasttime Addobject:dic]; Turn on timer method:- (void) starttimer{Timer= [Nstimer scheduledtimerwithtimeinterval:1Target:selfselector: @selector (refreshlesstime) UserInfo:@""Repeats:yes]; If you do not add the following statement, when UITableView is dragged, it will block the call of the timer [[Nsrunloop Currentrunloop] Addtimer:timer Formode:uitrackingrunloopmode]; } The main timer in the method, in the method, Traverse Totallasttime, take out the saved Lasttime and Indexpath,time used to display, after the display of the self-subtraction, Indexpath represents the corresponding display position, after a cycle, Replace the new time and unchanged Indexpath with the element in the corresponding position in the totallasttime to ensure that every second executes, the display is up-to-date. - (void) refreshlesstime{Nsuinteger time;  for(inti =0; i < Totallasttime.count; i++) { time= [[[[Totallasttime objectatindex:i] Objectforkey:@"Lasttime"]integervalue]; Nsindexpath*indexpath = [Nsindexpath indexpathforitem:0Insection:[[[totallasttime Objectatindex:i] Objectforkey:@"Indexpath"] [IntegerValue]]; Wlserviceordertableviewcell*cell = (Wlserviceordertableviewcell *) [_tableview Cellforrowatindexpath:indexpath]; Cell.remainingTimeLabel.text= [NSString stringWithFormat:@"remaining payment time:%@", [Self lesssecondtoday:--Time ]]; Nsdictionary*dic = @{@"Indexpath": [Nsstringstringwithformat:@"%i", Indexpath.section],@"Lasttime": [Nsstringstringwithformat:@"%i", Time]};    [Totallasttime replaceobjectatindex:i Withobject:dic]; }}-(NSString *) Lesssecondtoday: (nsuinteger) seconds{Nsuinteger Day= (Nsuinteger) seconds/( -*3600); Nsuinteger Hour= (Nsuinteger) (seconds% ( -*3600))/3600; Nsuinteger min= (Nsuinteger) (seconds% (3600))/ -; Nsuinteger Second= (Nsuinteger) (seconds% -); NSString*time = [NSString stringWithFormat:@"%lu Day%lu hours%lu minutes%lu seconds", (unsignedLong) Day, (unsignedLong) hour, (unsignedLong) min, (unsignedLong) second]; returnTime ;}

5: How to use method swizzling to insert some actions dynamically

Suppose there are many viewcontroller in the project, but there is an operation each page has, before each page is to write the same code, in fact, with method swizzling can solve the problem, For example Rdvtabbarcontroller this only in four home only to show, other pages are hidden;

A: Create an extension class: Uiviewcontroller+swizzle

Contents of the. h file:

#import <UIKit/UIKit.h><objc/runtime.h>"RDVTabBarController.h " @interface Uiviewcontroller (swizzle) @end

Contents of the. m File:

#import"uiviewcontroller+swizzle.h"@implementation Uiviewcontroller (swizzle)+ (void) load{SEL Origsel=@selector (viewdidappear:); SEL Swizsel=@selector (swiz_viewdidappear:); [Uiviewcontroller swizzlemethods:[selfclass] Originalselector:origsel Swizzledselector:swizsel]; SEL Vcwillappearsel=@selector (viewwillappear:); SEL Swizwillappearsel=@selector (swiz_viewwillappear:); [Uiviewcontroller swizzlemethods:[selfclass] Originalselector:vcwillappearsel Swizzledselector:swizwillappearsel];}+ (void) Swizzlemethods: (Class)classoriginalselector: (SEL) Origsel swizzledselector: (SEL) swizsel{Method origmethod= Class_getinstancemethod (class, Origsel); Method Swizmethod= Class_getinstancemethod (class, Swizsel); //Class_addmethod would fail if original method already existsBOOL Didaddmethod = Class_addmethod (class, Origsel, Method_getimplementation (Swizmethod), method_gettypeencoding (Swizmethod)); if(Didaddmethod) {Class_replacemethod (class, Swizsel, Method_getimplementation (Origmethod), method_gettypeencoding (Origmethod)); } Else {        //Origmethod and Swizmethod already existmethod_exchangeimplementations (Origmethod, Swizmethod); }}- (void) Swiz_viewdidappear: (BOOL) animated{//the controller name can be filtered to achieve filtering which is not operationalNSString *curclassname=nsstringfromclass ([selfclass]); if(curclassname.length>0&& ([Curclassname isequaltostring:@"Bdcustomerlistviewcontroller"]|| [Curclassname isequaltostring:@"Bdordersviewcontroller"]|| [Curclassname isequaltostring:@"Bddiscoverviewcontroller"]|| [Curclassname isequaltostring:@"Bdmineinfoviewcontroller"]) {[Self.rdv_tabbarcontroller settabbarhidden:no animated:yes]; }    //the code that needs to be injected is written here[self swiz_viewdidappear:animated];}-(void) Swiz_viewwillappear: (BOOL) animated{if([[Self.navigationcontroller childviewcontrollers] Count] >1) {[Self.rdv_tabbarcontroller settabbarhidden:yes animated:yes]; } [self swiz_viewwillappear:animated];} @end
Description: the + (void) Load method is a class method that, when the code of a class is read into memory, the runtime sends A + (void) load message to each class. So the + (void) Load method is a very early method to call, and the + (void) Load method is called to the Swizzling method, regardless of the parent class or the child class.

B: Call to introduce this extension class in MAIN.M

#import <UIKit/UIKit.h>"AppDelegate.h""  Uiviewcontroller+swizzle.h"int main (intChar * argv[]) {    @ Autoreleasepool {        returnclass]));    } }

6:ios on the tensile problems of Uiimageview

Specifying these 4 widths will result in a black module. This is the piece of content that is constantly filling the middle of the stretch.

uiimage* img=[uiimage imagenamed:@"2.png"];//OriginalUiedgeinsets Edge=uiedgeinsetsmake (0,Ten,0,Ten); //Uiimageresizingmodestretch: Stretch mode, fills the picture by stretching the rectangle area specified by the Uiedgeinsets to the left and right//Uiimageresizingmodetile: Tile mode, fills the map by repeating the uiedgeinsets specified rectangular areaimg=[img Resizableimagewithcapinsets:edge resizingmode:uiimageresizingmodestretch];self.imageview.image=img; application in Project: Self.myimageview=[[uiimageview Alloc]initwithframe:cgrectmake (0, -,184, -)]; UIImage*curimage=[uiimage imagenamed:@"Form_no_selected_icon"]; Uiedgeinsets Edge=uiedgeinsetsmake (0, +,0, -); Curimage=[Curimage Resizableimagewithcapinsets:edge Resizingmode:uiimageresizingmodestretch]; Self.myImageView.image=Curimage; [Self.view AddSubview:self.myImageView];

If just left and right stretching with Uiimageresizingmodestretch, about the value of the edge from a real size to determine, the block to stretch, in particular, pay attention to 2X and 3X pictures;

iOS Development Basics-Fragmentation 35

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.