iOS 各種傳值方式

來源:互聯網
上載者:User

標籤:

傳值方式

//單例//.h+ (Instannce *)shareInstance;//.mstatic Instannce *instance = nil;@implementation Instannce//定義一個建立單例對象的方法+ (Instannce *)shareInstance {    if (instance == nil) {        instance = [[Instannce alloc] init];    }       return instance;}//使用alloc的時候調用的方法instancetype+ (id)allocWithZone:(struct _NSZone *)zone {    if (instance == nil) {        instance = [super allocWithZone:zone];    }      return instance;}- (id)copy {    return self;}- (id)retain {    return self;}- (NSUInteger)retainCount {    //返回無符號最大值    return UINT_MAX;}- (oneway void)release {    //什麼也不做}//代理//.h@protocol GetMessageProtocol <NSObject >- (void)getNum:(NSString *)num withPassWord:(NSString *)pass;@end@property (nonatomic,assign) id<GetMessageProtocol> delegate;//.mif ([self.delegate respondsToSelector:@selector(getNum:withPassWord:)]) {        [self.delegate getNum:num.text withPassWord:passWord.text];    }#pragma mark - GetMessageProtocol- (void)getNum:(NSString *)num withPassWord:(NSString *)pass {    }registerCtrl.delegate = self;//通知 注意postNotificationName 必須一致    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationName object:self userInfo:dic];  //dic存放在userinfo中   dic中存放要傳過去的值是個字典//接受通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:NotificationName object:nil];//KVO監聽/*KVO觀察者方法 keyPath: 監聽的屬性名稱 object: 被觀察的對象 change: 屬性值 context: 上下裝置文 */    [registerCtrl addObserver:self forKeyPath:@"屬性名稱1" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];    [registerCtrl addObserver:self forKeyPath:@"屬性名稱2" options:NSKeyValueObservingOptionNew context:nil];//觸發的事件- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    //object的值是registerCtrl    if ([keyPath isEqualToString:@"屬性名稱1"]) {           }else if ([keyPath isEqualToString:@"屬性名稱2"]) {            }  }//.h@property (nonatomic, copy) NSString *屬性名稱1;@property (nonatomic, copy) NSString *屬性名稱2;//.m   必須通過setter方法改變值或者KVC//KVO方式//觸發的事件[indexCollectionView addObserver:self forKeyPath:@"屬性名稱" options:NSKeyValueObservingOptionNew context:nil];        [posterCollectionView addObserver:self forKeyPath:@"pathIndex" options:NSKeyValueObservingOptionNew context:nil];- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    //得到改變後的新值    NSInteger index = [[change objectForKey:@"new"] integerValue];    }}//Block  block的傳回值  block的名稱    block的參數    typedef void(^SucccessBlock)(NSString *); //Block的定義@property(nonatomic,copy)SucccessBlock loginBlock;  //block的聲明  要用copy防止block的循環參考_freindBlcok(friends); block的調用[[MyXMPPManager shareManager] getFreind:^(NSArray *freinds) {}   //block的賦值 實現

iOS 各種傳值方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.