汉子转拼音 + View上按钮响应Controller事件

汉字转拼音方法如下:###

屏幕快照 2016-05-16 下午10.09.04.png

去掉拼音和不去掉拼音两种方法:

- (NSString *)transform:(NSString *)chinese{
    //将NSString装换成NSMutableString
    NSMutableString *pinyin = [chinese mutableCopy];
    //将汉字转换为拼音(带音标)
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    NSLog(@"%@", pinyin);
    //去掉拼音的音标
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    NSLog(@"%@", pinyin);
    //返回最近结果
    return pinyin;
}

View上响应Controller事件###

两种思路:
一 Block回调方法

typedef void(^MyBlock)(void);
@property(nonatomic,copy)MyBlock myBlock;

在按钮的事件中调用block

- (void)btnAction:(UIButton *)sender{
    
    if (self.myBlock) {
        
        self.myBlock();
    }
}

在Controller中回调

- (void)addBlockView{
    
    self.myBlockView = [[BlockView alloc]init];
    self.myBlockView.backgroundColor = [UIColor cyanColor];
    
    __unsafe_unretained ViewController *this = self;
    
    self.myBlockView.myBlock = ^(){
        
        [this enterTheHotel];
        this -> _myBlockView.backgroundColor = [UIColor redColor];
    };
    
    [self.view addSubview:self.myBlockView];
    
    [self.myBlockView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.emptyView);
        make.left.equalTo(self.emptyView.mas_right).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(100);

    }];
    

    
}

- (void)enterTheHotel{
    
    NSLog(@"进入了酒店");
}

二 Target
在View上添加Target初始化方法:

- (instancetype)initWithTarget:(id)target;

- (instancetype)initWithTarget:(id)target
{
    if (self = [super init]) {
        
        
        
        UIButton *tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [tipBtn setTitle:@"进入商城" forState:UIControlStateNormal];
        [tipBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        tipBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        tipBtn.layer.cornerRadius = 4;
        tipBtn.layer.borderColor = [UIColor grayColor].CGColor;
        tipBtn.layer.borderWidth = 1;
        tipBtn.layer.masksToBounds = YES;
        [self addSubview:tipBtn];
        

        if ([target respondsToSelector:@selector(enterMallAction)]) {
            
            [tipBtn addTarget:target action:@selector(enterMallAction) forControlEvents:UIControlEventTouchUpInside];
        }
        

        [tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(70, 23));
            make.centerY.equalTo(self);
//            make.right.equalTo(self.mas_centerX).offset(-10);
            make.centerX.equalTo(self);
        }];
        
        
    }
    
    return self;
}

在Controller中初始化View, 设置target为self, 那么Controller就会响应View的事件啦


    self.emptyView = [[ShopCartEmptyView alloc]initWithTarget:self];
    self.emptyView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.emptyView];
    
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.equalTo(self.view).offset(64);

        make.width.mas_equalTo(100);
        make.height.mas_equalTo(100);
        
    }];


// 进入商城响应事件
- (void)enterMallAction{
    
    NSLog(@"点击了进入商城");
}

在这里可以下载:源代码

你可能感兴趣的:(汉子转拼音 + View上按钮响应Controller事件)