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

Source: Internet
Author: User

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

1: General weakify and strongify

/*** Strong/weak reference conversion, used to solve the circular reference problem between the block and the strong reference self. * Call method: '@ weakify_self' implements weak reference conversion, '@ strongify_self' implements forced reference conversion ** example: * @ weakify_self * [obj block: ^ {* @ strongify_self * self. property = something; *}]; */# ifndef weakify_self # if _ has_feature (objc_arc) # define weakify_self autoreleasepool {}__ weak _ typeof _ (self) weakSelf = self; # else # define weakify_self autoreleasepool {}_ block _ typeof _ (self) blockSelf = self; # endif # ifndef strongify_self # if _ has_feature (objc_arc) # define strongify_self try {}@ finally {}_ _ typeof _ (weakSelf) self = weakSelf; # else # define strongify_self try {}@ finally {}_ _ typeof _ (blockSelf) self = blockSelf; # endif/***, used to solve the circular reference problem between a block and a strongly referenced object * Call method: '@ weakify (object)' to implement weak reference conversion, '@ strongify (object) 'example of implementing strong reference conversion **: * @ weakify (object) * [obj block: ^ {* @ strongify (object) * strong_object = something; *}]; */# ifndef weakify # if _ has_feature (objc_arc) # define weakify (object) autoreleasepool {}__ weak _ typeof _ (object) weak ##### object = object; # else # define weakify (object) autoreleasepool {}_ _ block _ typeof _ (object) block ##### object = object; # endif # ifndef strongify # if _ has_feature (objc_arc) # define strongify (object) try {}@ finally {}_ _ typeof _ (object) strong ##### object = weak ##### object; # else # define strongify (object) try {}@ finally {}_ _ typeof _ (object) strong ##### object = block ##### object; # endif

Use (the two macros are regarded as a pair, and weak is used before strong ):

@ Weakify (self); // defines the self_weak _ variable of _ weak [RACObserve (self, name) subscribeNext: ^ (NSString * name) {@ strongify (self ); // The local area defines a _ strong self pointer pointing to self_weak self. outputLabel. text = name;}];

 

2: The objc runtime dynamically adds attributes.

Note: A New attribute is added to the class extension. Generally, class extensions only allow adding methods. Objc/runtime. h must be introduced first, mainly in the following two methods:

Value assignment: OBJC_EXPORT void assign (id object, const void * key, id value, objc_AssociationPolicy policy) value: OBJC_EXPORT id objc_getAssociatedObject (id object, const void * key)

Instance (reference ):

UILabel+Associate.h#import <UIKit/UIKit.h>@interface UILabel (Associate)- (void) setFlashColor:(UIColor *) flashColor;- (UIColor *) getFlashColor;@endUILabel+Associate.m#import "UILabel+Associate.h"#import <objc/runtime.h>@implementation UILabel (Associate)static char flashColorKey;- (void) setFlashColor:(UIColor *) flashColor{    objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UIColor *) getFlashColor{   return objc_getAssociatedObject(self, &flashColorKey);}@end

Call code:

    UILabel *lab = [[UILabel alloc] init];    [lab setFlashColor:[UIColor redColor]];    NSLog(@"%@", [lab getFlashColor]);

 

3: navigationController popToViewController jumps to the upper layer

        NSUInteger index=self.navigationController.viewControllers.count-3;        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index] animated:YES];

If the animation is to flip the page, it may be because the current page has a keyboard. You can recycle the keyboard first, and then the action will turn back to the normal animation effect.

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.