Basic knowledge of IOS development-fragment 35, basic knowledge of ios-35

Source: Internet
Author: User

Basic knowledge of IOS development-fragment 35, basic knowledge of ios-35

1: iOS View Controls object Lifecycle

 

Init-initialization program

ViewDidLoad-load View

Called when the view of the viewWillAppear-UIViewController object is about to be added to the window;

Called when viewDidApper-UIViewController object view has been added to the window;

Call viewWillDisappear-when the view of the UIViewController object is about to disappear, be overwritten, or be hidden;

Called when the viewDidDisappear-UIViewController object's view disappears, is overwritten, or hidden;

 

Execution time sequence

15:51:44. 811 inHyron [483: b903] init

15:51:54. 081 inHyron [483: b903] viewDidLoad

15:51:54. 082 inHyron [483: b903] viewpaiappear

15:51:54. 084 inHyron [483: b903] viewDidAppear

Obviously, run init, viewDidLoad, viewWillAppear, and viewDidAppear first, so that the view is created. When the view disappears or is overwritten:

15:54:14. 557 inHyron [483: b903] viewWillDisappear

15:54:14. 558 inHyron [483: b903] viewDidDisappear

In this way, the view disappears.

 

2: Initialize an instance class with default values

UserManager. h file content: # 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 content: # import "userManager. h "@ implementation userManager-(instancetype) init {self = [super init]; if (self) {_ userName = @" root "; _ passWord = @" 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 call code: userManager * myManager = [userManager userManagerWithType: 4]; NSLog (@ "user information: % @", myManager. toGetParams );

 

3: NSHTTPCookieStorage (obtain and delete cookies)

CookieNSArray * cookiesArray = [[sharesharedhttpcookiestorage] cookies]; NSDictionary * cookieDict = [NSHTTPCookie keys: cookiesArray]; NSString * cookie = [cookieDict objectForKey: @ "Cookie"]; // set the cookie for the http header [urlRequest setValue: cookie forHTTPHeaderField: @ "Cookie"]; Delete cookieNSArray * cookiesArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; for (NSHTTPCookie * cookie in cookiesArray) {[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie: cookie];}

 

4: iOS implements different countdown implementations for multiple cells in UITableView

// All remaining time arrays NSMutableArray * totalLastTime; In all data requested by the network, save the remaining time in the data to be displayed in the countdown as needed, if all the data here requires countdown, you only need to save the time. If some data requires countdown, you can save the dictionary, the two key-value pairs are their indexPath in the UITableView and the remaining time: num starts from 0 by default. NSDictionary * dic ={ @ "indexPath": [NSStrin stringWithFormat: @ "% I", num], @ "lastTime": order. payLastTime}; [totalLastTime addObject: dic]; enable the timer method:-(void) startTimer {timer = [nst1_scheduledtimerwithtimeinterval: 1 target: Selfselector: @ selector (refreshLessTime) userInfo: @ "" repeats: YES]; if the following statement is not added, the call to the timer will be blocked. [[nsunloop currentRunLoop] addTimer: timer forMode: UITrackingRunLoopMode];} the methods in the main timer will be traversed in this method, totalLastTime, extract the stored lasttime and indexpath. time is used for display. After the display is complete, it is automatically subtracted. indexpath indicates the corresponding position. After a loop, replace the new time and the unchanged indexpath with the corresponding position in totalLastTime to ensure that the display time is updated every second of execution. -(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 hour % lu minute % lu second", (unsigned long) day, (unsigned long) hour, (unsigned long) min, (unsigned long) second]; return time ;}

 

5: How to Use Method Swizzling to dynamically insert some operations

Suppose there are many viewcontrollers in the project, but there is an operation on every page. In the past, the same code was written on every page. In fact, Method Swizzling can solve this problem, for example, RDVTabBarController is only displayed on four homepages, and other pages are hidden;

A: Create an extension class: UIViewController + Swizzle

. H file content:

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

. M file content:

# Import "UIViewController + Swizzle. h "@ implementation UIViewController (Swizzle) + (void) load {SEL origSel = @ selector (viewDidAppear :); SEL swizSel = @ selector (swiz_viewDidAppear :); [UIViewController swizzleMethods: [self class] originalSelector: origSel Timer: swizSel]; SEL vcWillAppearSel = @ selector (timer :); SEL swizWillAppearSel = @ selector (timer :); [UIViewController swizzleMethods: [self class] originalSelector: vcWillAppearSel Syntax: swizWillAppearSel];} + (void) swizzleMethods :( Class) class originalSelector :( SEL) origSel swizzledSelector :( SEL) swizSel {Method origMethod = compile (class, origSel ); method swizMethod = invoke (class, swizSel); // class_addMethod will fail if original method already exists BOOL didAddMethod = class_addMethod (class, origSel, transform (swizMethod), transform (swizMethod )); if (didAddMethod) {class_replaceMethod (class, swizSel, method_getImplementation (origMethod), method_getTypeEncoding (origMethod);} else {// origMethod and swizMethod already exist (method, swizMethod) ;}}- (void) swiz_viewDidAppear :( BOOL) animated {// The Controller name can be filtered to filter out which are not operated NSString * curClassName = NSStringFromClass ([self 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 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
Note: The + (void) load method is a class method. When the code of a class is read into the memory, runtime will send the + (void) load message to each class. Therefore, the + (void) load method is a method with a very early call time, And the + (void) load method will be called no matter whether the parent class or subclass, it is suitable for inserting swizzling methods.

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 questions about UIImageView stretching

 

After the four widths are specified, the black module is formed. The straightforward point is that the content is the continuously filled part in the middle of the stretch.

UIImage * img = [UIImage imageNamed: @ "2.png"]; // source image UIEdgeInsets edge = UIEdgeInsetsMake (0, 10,); // UIImageResizingModeStretch: stretch mode, by stretching the area of the rectangle specified by UIEdgeInsets to fill the upper, lower, and lower sides of the image. // tiled: The image img = [img resizableImageWithCapInsets: edge resizingMode: UIImageResizingModeStretch]; self. imageView. image = img; Project Application: self. myImageView = [[UIImageView alloc] initWithFrame: CGRectMake (0,100,184, 25)]; UIImage * curImage = [UIImage imageNamed: @ "Custom"]; UIEdgeInsets edge = UIEdgeInsetsMake (0, 40, 0, 25); curImage = [curImage resizableImageWithCapInsets: edge resizingMode: UIImageResizingModeStretch]; self. myImageView. image = curImage; [self. view addSubview: self. myImageView];

Use UIImageResizingModeStretch to stretch the left and right sides of the edge according to the actual size. Use this part for stretching. Pay special attention to the 2X and 3X images;

Related Article

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.