IOS開發基礎知識--片段28,ios基礎知識--28

來源:互聯網
上載者:User

IOS開發基礎知識--片段28,ios基礎知識--28

1:通用的weakify和strongify

/*** 強弱引用轉換,用於解決代碼塊(block)與強引用self之間的循環參考問題* 調用方式: `@weakify_self`實現弱引用轉換,`@strongify_self`實現強引用轉換** 樣本:* @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#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#endif/*** 強弱引用轉換,用於解決代碼塊(block)與強引用對象之間的循環參考問題* 調用方式: `@weakify(object)`實現弱引用轉換,`@strongify(object)`實現強引用轉換** 樣本:* @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#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#endif

 運用(這兩個宏一定成對出現,先weak再strong):

@weakify(self); // 定義了一個__weak的self_weak_變數 [RACObserve(self, name) subscribeNext:^(NSString *name) {     @strongify(self); // 局域定義了一個__strong的self指標指向self_weak     self.outputLabel.text = name; }]; 

 

2:objc runtime 動態增加屬性

說明:給類擴充增加了一個新屬性。通常下類擴充只允許添加方法。必須要先引入 objc/runtime.h,主要是下面兩個方法:

賦值:OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)獲得值:OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)

執行個體(引用):

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

調用代碼:

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

 

3:navigationController popToViewController跳轉到上上層

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

如果動畫是翻轉頁面,有可能是因為當前頁面有鍵盤,可以先把鍵盤迴收後,動作就變成正常的回退動畫效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.