(1)多个按钮实现单选
-(void)btnPressed:(UIButton *)btn { for (int i = 0; i < 3; i++) { UIButton *button = (UIButton *)[[btn superview]viewWithTag:1000 + i]; [button setSelected:NO]; [button setTitleColor:SUBMIT_PAY_NUM_COLOR forState:UIControlStateNormal]; } self.Selectbutton = (UIButton *)btn; [self.Selectbutton setSelected:YES]; [self.Selectbutton setTitleColor:[UIColor colorWithHexString:@"0xfdd000"] forState:UIControlStateNormal]; }
(2)获取设备IP地址
// 获取设备IP地址 -(NSString *)getIPAddress { NSString *address = @"error"; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // 检索当前接口,在成功时,返回0 success = getifaddrs(&interfaces); if (success == 0) { // 循环链表的接口 temp_addr = interfaces; while(temp_addr != NULL) { if(temp_addr->ifa_addr->sa_family == AF_INET) { // 检查接口是否en0 wifi连接在iPhone上 if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // 得到NSString从C字符串 address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // 释放内存 freeifaddrs(interfaces); return address; }
(3)MD5加密
+ (NSString *)MD5:(NSString *)string{ const char* input = [string UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(input, (CC_LONG)strlen(input), result); NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [digest appendFormat:@"%02x", result[i]]; } return digest; }
(4)获取父控制器
id object = [self nextResponder]; while (![object isKindOfClass:[UIViewController class]] && object != nil) { object = [object nextResponder]; } UIViewController *superController = (UIViewController*)object;
(5)打印Model每个字段值
- (NSString *)description { //初始化一个字典 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //得到当前class的所有属性 uint count; objc_property_t *properties = class_copyPropertyList([self class], &count); //循环并用KVC得到每个属性的值 for (int i = 0; i<count; i++) { objc_property_t property = properties[i]; NSString *name = @(property_getName(property)); id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串 [dictionary setObject:value forKey:name];//装载到字典里 } //释放 free(properties); //return return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary]; }
(6)UIButton设置:图片在上,文字在下
//图片在上,文字在下 - (void)setButtonImageAndTitleWithSpace:(CGFloat)spacing WithButton:(UIButton *)btn{ CGSize imageSize = btn.imageView.frame.size; CGSize titleSize = btn.titleLabel.frame.size; CGSize textSize = [btn.titleLabel.text sizeWithFont:btn.titleLabel.font]; CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height)); if (titleSize.width + 0.5 < frameSize.width) { titleSize.width = frameSize.width; } CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); btn.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width); btn.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0); }
(7)数组是否为空
if (self.selectedModelArray != nil && ![self.selectedModelArray isKindOfClass:[NSNull class]] && self.selectedModelArray.count != 0)
(8)是否包含某个字符串
-(NSMutableString *)stringRang:(NSString *)str { NSRange range; range = [str rangeOfString:@"amp;"]; NSMutableString *resultString = [NSMutableString string]; if (range.location != NSNotFound) { NSLog(@"found at location = %lu, length = %lu",(unsigned long)range.location,(unsigned long)range.length); NSString *app_id = [str substringToIndex:range.location]; NSString *biz_content = [str substringFromIndex:range.location + range.length]; resultString = [app_id stringByAppendingString:biz_content]; } else { NSLog(@"Not Found"); } return resultString; }