小题大做

1.通知。
A.定义
a、通知传值

[[NSNotificationCenter defaultCenter]postNotificationName:@"RequestData" object:[NSNumber numberWithInteger:self.PraiseNum] userInfo:self.myDic];

b、仅仅通知

[[NSNotificationCenter defaultCenter]postNotificationName:@"唯一通知标识" object:nil];

B,接收通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadRuestData:) name:@"RequestData" object:nil];

- (void)reloadRuestData:(NSNotification *)aNotification

{

NSInteger Pint = [aNotification.object integerValue];

NSDictionary *DD = aNotification.userInfo;

}

 

2.得到当前点击的这个cell

PostThreeImageCell *cell = (PostThreeImageCell *)btn.superview.superview.superview.superview;(根据自己这个控件所在的层。添加或者减少superview。)

NSIndexPath *indexPost = [_tableView indexPathForCell:cell];

 

3.正则判断字符格式

//判断字符串的格式,只能由字母、数字、下划线组成,并且只能以字母开头

NSString *str = @"^[a-zA-Z]\\w*$";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];

BOOL isMatch = [pred evaluateWithObject:txt1.text];

if (isMatch==NO) {

alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"字符只能由字母、数字、下划线组成,并且由字母开头。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

[alert show];

}else{

NSLog(@"格式正确");

}

 

4.根据传入的时间字符串判断时间距当前处于什么时刻

/*

 判断时间的显示格式

今天就显示:时分

昨天就显示:昨天

这周就显示:星期几

这周以前:年月日

 */

-(NSString *)compareDate:(NSString *)time{

 

NSLog(@"time si %@",time);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];

NSDate *date = [dateFormat dateFromString:time];

 

int timeIntervar=[date timeIntervalSinceNow];

 

NSTimeInterval secondsPerDay = 24 * 60 * 60;

NSDate *today = [[NSDate alloc] init];

NSDate *tomorrow, *yesterday;

 

tomorrow = [today dateByAddingTimeInterval: secondsPerDay];

yesterday = [today dateByAddingTimeInterval: -secondsPerDay];

 

NSString * todayString = [[today description] substringToIndex:10];

NSString * yesterdayString = [[yesterday description] substringToIndex:10];

NSString * tomorrowString = [[tomorrow description] substringToIndex:10];

NSString * dateString = [[date description] substringToIndex:10];

 

//判断周几

NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents* components = [[NSDateComponents alloc] init];

NSInteger unitFlags = NSYearCalendarUnit |

NSMonthCalendarUnit |

NSDayCalendarUnit |

NSWeekdayCalendarUnit |

NSHourCalendarUnit |

NSMinuteCalendarUnit |

NSSecondCalendarUnit;

NSLog(@"date is %@",date);

components = [calendar components:unitFlags fromDate:date];

NSUInteger weekday = [components weekday];

NSLog(@"weekday si %d",weekday);

 

//判断是今天还是昨天

if ([dateString isEqualToString:todayString])

{

//今天

[dateFormat setDateFormat:@"HH:mm"];//设定时间格式

return [dateFormat stringFromDate:date];

} else if ([dateString isEqualToString:yesterdayString])

{

return @"昨天";

}else if ([dateString isEqualToString:tomorrowString])

{

return @"明天";

}

else

{

//判断是否是一周前

if(-timeIntervar>604800){

[dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式

NSString *dateString = [dateFormat stringFromDate:date];

return dateString;

}else{

//return dateString;

NSLog(@"weekday si %d",weekday);

//判断周几

switch (weekday) {

 case 2:

 return @"星期一";

 break;

 case 3:

 return @"星期二";

 break;

 case 4:

 return @"星期三";

 break;

 case 5:

 return @"星期四";

 break;

 case 6:

 return @"星期五";

 break;

 case 7:

 return @"星期六";

 break;

 case 1:

 return @"星期日";

 break;

 default: 

 break;

}

}

}

 

5.获取设备唯一编号

+ (NSString *)getDeviceId

{

    NSString *udid = [UIDevice uniqueDeviceIdentifier];

    NSLog(@"udid in keychain %@", udid);

    return udid;

}

 

6.键盘出现时,向上移动view,防止遮住输入框

//开始编辑输入框的时候,软键盘出现,执行此事件

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

CGRect frame = textField.frame;

int offset = frame.origin.y +100 - (self.view.frame.size.height - 216.0);//键盘高度216   (100这个根据自己的情况而定)

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

if(offset < 0)

self.view.frame = CGRectMake(0.0f, offset, self.view.frame.size.width, self.view.frame.size.height);

[UIView commitAnimations];

}

 

//当用户按下return键或者按回车键,keyboard消失

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

[textField resignFirstResponder];

 //判断ios版本

  if ([[[UIDevice currentDevice]systemVersion]doubleValue]>=7.0) {

   self.view.frame = [UIScreen mainScreen].bounds;

  }else{

   self.view.frame = [UIScreen mainScreen].applicationFrame;   

  }

return YES;

}

//结束编辑

-(void)textFieldDidEndEditing:(UITextField *)textField{

 //判断ios版本

  if ([[[UIDevice currentDevice]systemVersion]doubleValue]>=7.0) {

   self.view.frame = [UIScreen mainScreen].bounds;

  }else{

   self.view.frame = [UIScreen mainScreen].applicationFrame;

  }

}

#pragma -mark  隐藏键盘

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[txt2 resignFirstResponder];

 //判断ios版本

  if ([[[UIDevice currentDevice]systemVersion]doubleValue]>=7.0) {

   self.view.frame = [UIScreen mainScreen].bounds;

  }else{

   self.view.frame = [UIScreen mainScreen].applicationFrame;

  }

}

 

七、控制UITextField的字符个数(此例最多140字符)

在ViewDidLoad里或ViewDidAppear里写通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged:)

name:UITextViewTextDidChangeNotification

object:txtDetail];

然后:

-(void)textViewEditChanged:(NSNotification *)obj{

  SSTextView *textField = (SSTextView *)obj.object;

  [self textViewChange:textField.text];

}

 

-(void)textViewChange:(NSString *)newText

{

  NSString *toBeString = newText;

  NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式

  if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写

  UITextRange *selectedRange = [txtDetail markedTextRange];

  //获取高亮部分

  UITextPosition *position = [txtDetail positionFromPosition:selectedRange.start offset:0];

  // 没有高亮选择的字,则对已输入的文字进行字数统计和限制

  if (!position) {

    if (toBeString.length > 140) {

      txtDetail.text = [toBeString substringToIndex:140];

    if (nums==1) {

      [alert show];

    }

      nums++;

  }

}

  // 有高亮选择的字符串,则暂不对文字进行统计和限制

  else{

  }

}

  // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况

  else{

    if (toBeString.length > 140) {

      txtDetail.text = [toBeString substringToIndex:140];

    }

}

}

 

posted @ 2014-12-05 14:27  xm心中的MV  阅读(173)  评论(0编辑  收藏  举报