iOS关联的一些实际用法

   之前写了一些关联的一些基本用法,有些朋友说理论性太强,没有实际的Demo。于是在UIAlertView上写了一个Demo.可以实现UIAlertView的回调操作.具体如下

Category.h

//
//  UIAlertView+yw.h
//  UIAlterViewDemo
//
//  Created by yuanwei on 15-4-2.
//  Copyright (c) 2015年 YuanWei. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void(^yw_void_block_int)(int index);

@interface UIAlertView (yw)<UIAlertViewDelegate>

+ (void)dismiss:(BOOL)animated;

+ (UIAlertView *)showAlertWithTitle:(NSString *)title
                            message:(NSString *)message
                            buttons:(NSArray  *)buttons
                       handlerBlock:(yw_void_block_int)handlerBlock;

+ (UIAlertView *)initWithTitle:(NSString *)title
                       message:(NSString *)message
                       buttons:(NSArray  *)buttons;

- (void)setCompletionHandlerBlock:(yw_void_block_int)handlerBlcok;

- (void)dismiss:(BOOL)animated;

@end
Category.m
//
//  UIAlertView+yw.m
//  UIAlterViewDemo
//
//  Created by yuanwei on 15-4-2.
//  Copyright (c) 2015年 YuanWei. All rights reserved.
//

#import "UIAlertView+yw.h"
#import <objc/runtime.h>

#define AlertCompletionHandlerBlock  @"AlertCompletionHandlerBlock"

static UIAlertView *ywAlertView;


//两种写法都行
static char yw_InjectDict_Key;
//const void *yw_InjectDict_Key;

//也可以直接这样写
//const void *yw_InjectDict_Key = &yw_InjectDict_Key;

@implementation UIAlertView (yw)


+ (void)dismiss:(BOOL)animated
{
    if (ywAlertView) {
        [ywAlertView dismiss:animated];
    }
}

+ (UIAlertView *)showAlertWithTitle:(NSString *)title
                           message:(NSString *)message
                           buttons:(NSArray  *)buttons
                      handlerBlock:(yw_void_block_int)handlerBlock
{
    UIAlertView *alertView = [self initWithTitle:title
                                         message:message
                                         buttons:buttons];
    
    [alertView setCompletionHandlerBlock:handlerBlock];
    
    [alertView show];
    
    return alertView;
}

+ (UIAlertView *)initWithTitle:(NSString *)title
                      message:(NSString *)message
                      buttons:(NSArray  *)buttons
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil];
    
    for (NSString *btnName in buttons){
        [alertView addButtonWithTitle:btnName];
    }
    
    //防止和正常的业务逻辑冲突
    alertView.cancelButtonIndex = -1;
    
    ywAlertView = alertView;

    return alertView;
}

- (yw_void_block_int)getCompletionHandlerBlock
{
    yw_void_block_int handlerBlcok = [self getInjectBlockByIdentifier:AlertCompletionHandlerBlock];
    
    return handlerBlcok;
}
- (id)getInjectBlockByIdentifier:(NSString *)identifier
{
    id obj = nil;
    
    NSAssert(identifier != nil, @"根据关键字取关联对象的值。关键不能为空");
    
    NSDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
    if(injectDict){
        obj = injectDict[identifier];
    }
    
    return obj;
}

- (void)setCompletionHandlerBlock:(yw_void_block_int)handlerBlcok
{
    self.delegate = (id<UIAlertViewDelegate>)self;
    
    [self injectBlock:handlerBlcok withIdentifier:AlertCompletionHandlerBlock];
}

- (void)injectBlock:(id)block withIdentifier:(NSString *)identifier
{
    if (block) {
        id newBlock = [block copy];
        NSAssert(identifier != nil, @"创建关联,关键字不能为空");
        
        NSMutableDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
        if (!injectDict) {
            injectDict = [NSMutableDictionary dictionary];
            objc_setAssociatedObject(self, &yw_InjectDict_Key, injectDict, OBJC_ASSOCIATION_RETAIN);
        }
        
        injectDict[identifier] = newBlock;
    }
}


- (void)dismiss:(BOOL)animated
{
    [self dismissWithClickedButtonIndex:self.cancelButtonIndex animated:animated];
}


#pragma mark - UIAlertViewDelegateMethod

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    yw_void_block_int handlerBlcok = [self getCompletionHandlerBlock];
    
    if(handlerBlcok){
        handlerBlcok((int)buttonIndex);
    }
    
    [self removeInjectBlockByIdentifier:AlertCompletionHandlerBlock];
}

- (void)removeInjectBlockByIdentifier:(NSString *)identifier
{
    NSAssert(identifier != nil, @"根据关键字,移除对应的关联值,关键字不能为空");
    
    NSMutableDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
    if(injectDict){
        [injectDict removeObjectForKey:identifier];
    }
}

@end


ViewController.m中的用法


//
//  ViewController.m
//  UIAlterViewDemo
//
//  Created by yuanwei on 15-4-2.
//  Copyright (c) 2015年 YuanWei. All rights reserved.
//

#import "ViewController.h"
#import "UIAlertView+yw.h"

@interface ViewController ()

@end

@implementation ViewController


- (IBAction)handClick:(id)sender
{
    [UIAlertView showAlertWithTitle:nil
                            message:@"请选择出行方式"
                            buttons:@[@"火车",@"飞机",@"取消"]
                       handlerBlock:^(int index) {
                           
                           switch (index) {
                               case 0:{
                                   
                                   NSLog(@"%d %@",index,@"火车");
                                   
                               }break;
                               case 1:{
                                   
                                   NSLog(@"%d %@",index,@"飞机");
                                   
                               }break;
                                   
                               default:
                                   break;
                           }
                       }];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


附上Demo下载链接:http://download.csdn.net/detail/u014466582/8557275

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值