漢字轉(zhuǎn)拼音方法如下:###
屏幕快照 2016-05-16 下午10.09.04.png
去掉拼音和不去掉拼音兩種方法:
- (NSString *)transform:(NSString *)chinese{
//將NSString裝換成NSMutableString
NSMutableString *pinyin = [chinese mutableCopy];
//將漢字轉(zhuǎn)換為拼音(帶音標(biāo))
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
NSLog(@"%@", pinyin);
//去掉拼音的音標(biāo)
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
NSLog(@"%@", pinyin);
//返回最近結(jié)果
return pinyin;
}
View上響應(yīng)Controller事件###
兩種思路:
一 Block回調(diào)方法
typedef void(^MyBlock)(void);
@property(nonatomic,copy)MyBlock myBlock;
在按鈕的事件中調(diào)用block
- (void)btnAction:(UIButton *)sender{
if (self.myBlock) {
self.myBlock();
}
}
在Controller中回調(diào)
- (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(@"進(jìn)入了酒店");
}
二 Target
在View上添加Target初始化方法:
- (instancetype)initWithTarget:(id)target;
- (instancetype)initWithTarget:(id)target
{
if (self = [super init]) {
UIButton *tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tipBtn setTitle:@"進(jìn)入商城" 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, 設(shè)置target為self, 那么Controller就會(huì)響應(yīng)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);
}];
// 進(jìn)入商城響應(yīng)事件
- (void)enterMallAction{
NSLog(@"點(diǎn)擊了進(jìn)入商城");
}
在這里可以下載:源代碼