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: Initialize a default value instance class
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) {[email protected] "root"; [email protected] "123456"; _type = 0; } return self;} + (Usermanager *) Usermanagerwithtype: (nsinteger) type{Usermanager *mymanager=[[usermanager Alloc]init]; Mymanager.type=type; return Mymanager;} -(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)
Remove Cookiensarray *cookiesarray = [[Nshttpcookiestorage sharedhttpcookiestorage] cookies]; Nsdictionary *cookiedict = [Nshttpcookie Requestheaderfieldswithcookies:cookiesarray]; NSString *cookie = [cookiedict objectforkey:@ "Cookie"];//set HTTP header 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 array nsmutablearray *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 all the data is required to be counted down, it is only necessary to save the time , if there is some data to need a countdown, you can save the dictionary, two key-value pairs for its uitableview in the Indexpath and time remaining: num By default starting from 0 nsdictionary *dic = @{@ "Indexpath": [Nsstrin stringwithformat:@ "%i", num],@ "Lasttime": Order.paylasttime}; [Totallasttime Addobject:dic]; Turn on timer method:-(void) starttimer{timer = [Nstimer scheduledtimerwithtimeinterval:1 target: 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 (int i = 0; i < Totallasttime.count; i++) {time = [[[Totallasttime objectatindex:i] objectforkey:@ lasttime []integervalue]; Nsindexpath *indexpath = [Nsindexpath Indexpathforitem:0 insection:[[[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/(24*3600); Nsuinteger hour = (Nsuinteger) (seconds% (24*3600))/3600; Nsuinteger min = (nsuinteger) (seconds% (3600))/60; Nsuinteger second = (Nsuinteger) (seconds%60); NSString *time = [NSString stringwithformat:@ "%lu Day%lu hours%lu minutes%lu seconds", (unsigned long) day, (unsigned long) hour, (unsigned Long) min, (unsigned long) second]; return time; }
Code used in the project:-(void) starttimer{Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector ( Refreshlesstime) userinfo:@ "" Repeats:yes]; [[Nsrunloop Currentrunloop] AddTimer:self.timer Formode:uitrackingrunloopmode]; }-(void) refreshlesstime{Nsinteger time; for (int i = 0; i < Self.totalLastTime.count; i++) {time = [[[Self.totallasttime objectatindex:i] Objectforkey: @ "Lasttime"]integervalue]; Nsindexpath *indexpath = [Nsindexpath indexpathforitem:[[[self.totallasttime objectatindex:i] objectForKey:@ " Indexpath "] integervalue] insection:0]; Bdfindactivitycell *cell = (Bdfindactivitycell *) [_tableview Cellforrowatindexpath:indexpath]; if (time==0) {[cell setcelldata:@ ' activity]; [Email protected] "is robbing a single"; cell.clockimageview.hidden=yes;//[Self.tableview Reloadrowsatindexpaths:[nsarray ArrayWithObject:indexPath] wi Throwanimation:uitableviewrowanimationfade]; --time; } else if (time>0) {cell.myLabel.text = [nsstring stringwithformat:@ "%@", [Self lesssecondtoday :--time]]; } nsdictionary *dic = @{@ "Indexpath": [NSString stringwithformat:@ "%ld", indexpath.row],@ "Lasttime": [NSString Strin gwithformat:@ "%li", Time]}; [Self.totallasttime replaceobjectatindex:i Withobject:dic]; }}-(NSString *) Lesssecondtoday: (Nsuinteger) seconds{Nsuinteger hour = (Nsuinteger) (seconds% (24*3600))/3600; Nsuinteger min = (nsuinteger) (seconds% (3600))/60; Nsuinteger second = (Nsuinteger) (seconds%60); NSString *time = [NSString stringwithformat:@ "to rob Single%02lu:%02lu:%02lu", (unsigned long) hour, (unsigned long) min, (unsigned Long) second]; return time; }
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> #import <objc/runtime.h> #import "RDVTabBarController.h" @interface Uiviewcontroller (Swizzle) @end
Contents of the. m File:
#import "Uiviewcontroller+swizzle.h" @implementation Uiviewcontroller (swizzle) + (void) load{SEL Origsel = @selector (vi Ewdidappear:); SEL Swizsel = @selector (swiz_viewdidappear:); [Uiviewcontroller Swizzlemethods:[self class] Originalselector:origsel Swizzledselector:swizsel]; SEL [email protected] (viewwillappear:); SEL [email protected] (swiz_viewwillappear:); [Uiviewcontroller Swizzlemethods:[self class] Originalselector:vcwillappearsel Swizzledselector:swizwillappearsel] ;} + (void) Swizzlemethods: (Class) Class Originalselector: (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 exists BOOL Didaddmethod = Class_addmethod (class, Origsel, Metho D_getimplementation (Swizmethod), method_gettypeencoding (Swizmethod)); if (Didaddmethod) {Class_replacemethod (class, Swizsel,Method_getimplementation (Origmethod), method_gettypeencoding (Origmethod)); } else {//origmethod and Swizmethod already exist method_exchangeimplementations (Origmethod, Swizmethod); }}-(void) Swiz_viewdidappear: (BOOL) animated{//can filter the controller name to achieve filtering which is not operating nsstring *curclassname=nsstringfromclass ([SE LF class]); 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> #import "AppDelegate.h" #import "uiviewcontroller+swizzle.h" int main (int argc, char * argv []) { @autoreleasepool { return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])); } }
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"];//original Uiedgeinsets edge=uiedgeinsetsmake (0, 0,10); Uiimageresizingmodestretch: Stretch mode, fills the picture by stretching uiedgeinsets the specified rectangular area //uiimageresizingmodetile: Tile mode, Fills the figure img= [img Resizableimagewithcapinsets:edge Resizingmode:uiimageresizingmodestretch] by repeating the uiedgeinsets specified rectangular area; self.imageview.image=img; application in Project: Self.myimageview=[[uiimageview alloc]initwithframe:cgrectmake (0, 100, 184, 25 )]; UIImage *curimage=[uiimage imagenamed:@ "Form_no_selected_icon"]; Uiedgeinsets edge=uiedgeinsetsmake (0, 0,25); 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;
7:appdelegate various events and some corresponding notification application
A activity-"inactive" uses applicationwillresignactive:/uiapplicationwillresignactivenotification to "pause" the display of the application. Make sure that you do not need timely user input in your work because the application will not get any user input for a period of time.
B Inactive-"Backstage use applicationdidenterbackground:/ Uiapplicationdidenterbackgroundnotification frees any resources (such as cached images or other data that can be easily loaded) that do not need to be persisted when the application is in the background state. or any resources (such as network connections) that cannot be saved in the background. Avoiding excessive use of memory here will make the application's final pause snapshot smaller, thereby reducing the overall risk of application from RAM. You can also use this opportunity to save any necessary application data that will help users find the progress of their last departure at the next restart.
C background-"Inactive uses applicationdidbecomeactive:/uiapplicationdidbecomeactivenotification to recover any actions that are performed when switching from the background state to the inactive state." For example, a persistent network connection can be re-established here.
D inactivity-the activity uses applicationdidbecomeactive:/uiapplicationdidbecomeactivenotification to recover any action that is made when it is never active to the active state. This method and notification is used when the application is completely new, so any operations performed here must also be valid in that context. Note: During this excessive process, the system does not provide a significant amount of time to save the changes here, providing only 5 seconds of time. If more than 5 seconds, the application will be immediately clear from memory and into the non-running state!
Take the uiapplicationdidbecomeactivenotification notification as an example, create a notification on the page and implement its code:
@interface Firstviewcontroller () @end @implementation firstviewcontroller-(void) viewdidload { [Super Viewdidload ]; Self.view.backgroundcolor=[uicolor Redcolor]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (didbecomeactivenotification:) Name:uiapplicationdidbecomeactivenotification Object:nil];} -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) Didbecomeactivenotification: (nsnotification *) note{ NSLog (@ "haha I entered");} @end
The execution of the above time for the program to run on this page, and then back to the background, and then into the app to wake up the page, will execute the above Didbecomeactivenotification method, if not on this page, even if the wake will not be executed
8:aspects uses it for cutting-plane programming.
Aspects Plugin Address: https://github.com/steipete/Aspects
Create a Swizzlemanager class:
. h file Contents: #import <Foundation/Foundation.h> #import "Aspects/aspects.h" @interface swizzlemanager:nsobject+ (void ) Createallhooks; @end. m file contents: #import "SwizzleManager.h" #import <UIKit/UIKit.h> @implementation swizzlemanager+ (void) createallhooks{[Uiviewcontroller aspect_hookselector: @selector (viewdidload) withopt Ions:aspectpositionbefore usingblock:^ (id<aspectinfo> info) { The user statistics code is written here NSLog (@ "[ASPECT] Inject in class instance:%@", [info instance]); } Error:null]; Other hooks ... goes ...} @end then in the Appdelegate file:-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (NSDictionary *) Launchoptions {//Override point for customization after application launch. Create [Swizzlemanager Createallhooks] within the first time the program starts; return YES;}
Good article: http://tech.glowing.com/cn/method-swizzling-aop/inside the instance can record each page, and each key event
9: How to change the color and position of Uitextfield placeholder?
Inherit Uitextfield, override this method
-(void) Drawplaceholderinrect: (cgrect) rect { [[Uicolor Bluecolor] setfill]; [Self.placeholder drawinrect:rect withFont:self.font linebreakmode:uilinebreakmodetailtruncation Alignment: Self.textalignment];}
10: How to change the color of the small tick of the cell in TableView to another color?
_mtableview.tintcolor = [Uicolor Redcolor];
iOS Development Basics-Fragmentation 35