1.如何用NSLog輸出NSRange,CGRect等結構體ios
NSString 中的方法:git
NSStringFromCGPoint
NSStringFromCGSize
NSStringFromCGRect
NSStringFromCGAffineTransform
NSStringFromUIEdgeInsets
正則表達式
如:NSLog(@"rect1: %@", NSStringFromCGRect(rect1));
數組
2.如何在navigationviewcontroller中,pop到以前不一樣的viewcontroller(push過的viewcontroller)?dom
每當咱們push到一個viewcontroller時,就會把這個viewcontroller的實例保存到NSArray裏,經過array能夠獲取到任何一個viewcontroller。ide
NSArray *viewControllers=[self.navigationControllerviewControllers]; UIViewController *controller=[viewControllers objectAtIndex:1]; [self.navigationControllerpopToViewController:controller animated:YES];
3,圖片模糊化處理
函數
+(UIImage *)scale:(UIImage *)p_w_picpath toSize:(CGSize)size { UIGraphicsBeginImageContext(size); [p_w_picpath drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
4.如何使用 NSNotificationCenter 在viewcontroller之間進行傳值?post
簡單點的來,兩個界面間傳值,直接上代碼了:ui
sendViewcontroller.mlua
//SettingViewController :接受值的viewcontroller SettingViewController *setting = [[SettingViewController alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:setting selector:@selector(received:) name:@"msetting" object:nil]; NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"user",@"type", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"msetting" object:dict]; [self.navigationController pushViewController:setting animated:YES]; [setting release];
SettingViewController.m(接收值的viewcontroller)
-(void)received:(NSNotification *)notification{ id data = [notification object]; NSLog(@"received data: %@",data); }
這樣就實現了基本的使用,跟delegate相似,注意 addObserver時,須要寫目標viewcontroller的實例,而不是self。
5.經過系統自帶的NSPredicate使用正則表達式。(在TextField中)
NSString *regex =[NSString stringWithFormat:@"^1(3[4-9]|5[012789]|8[2378]|47)\\d{8}$"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; BOOL isMatch = [predicate evaluateWithObject:editPhoneField.text];
6.如何限制UITextField輸入長度(監聽textField文本變化的事件)
一、實現UITextFieldDelegate協議;
二、實現textField:shouldChangeCharactersInRange:replacementString:方法;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int kMaxLength = 11; NSInteger strLength = textField.text.length - range.length + string.length; //輸入內容的長度 - textfield區域字符長度(通常=輸入字符長度)+替換的字符長度(通常爲0) return (strLength <= kMaxLength); }
如上代碼,若是咱們簡單的這樣寫: if(range.location<=11) return 或是 if (textfield.text.length>=11) 這樣雖然也能限制位數爲11位,可是若是經過放大鏡把光標切換到以前的位數後,你照樣能夠輸入,而且還會致使輸入11位後,鍵盤上的退格(X鍵)沒法使用,緣由是:咱們在location到達11位後,返回了NO,鍵盤沒法相應:添加,修改,刪除。這是很嚴重的。因此照着我上面的。
方法解讀:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
功能:
把textField中位置爲range的字符串替換爲string字符串;
此函數在textField內容被修改時調用;
返回值:
YES,表示修改生效;NO,表示不作修改,textField的內容不變。
參數說明:
textField:響應UITextFieldDelegate協議的UITextField控件。
range: UITextField控件中光標選中的字符串,即被替換的字符串;
range.length爲0時,表示在位置range.location插入string。
string: 替換字符串; string.length爲0時,表示刪除。
7.使用ios5.0之後的一個方法自定義table View Cell
UINib *nib = [UINib nibWithNibName:@"TvWeiboCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
8.獲取全局的Delegate對象,這樣咱們能夠調用這個對象的方法和變量
[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];
9.獲取截屏
- (UIImage *)getScreenShot { UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *p_w_picpath = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return p_w_picpath; }
10.貨幣格式轉
方法一:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setGroupingSeparator:@","]; [numberFormatter setGroupingSize:3]; [numberFormatter setUsesGroupingSeparator:YES]; [numberFormatter setDecimalSeparator:@"."]; [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSString *theString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:1008977.72]];
方法二:
-(NSString*)showPrice:(NSString*)price { NSMutableString* price1 =[[NSMutableString alloc]initWithString: price]; if (price.length>3) { for (int i=0; i<(price.length-1)/3; i++) { [price1 insertString:@"," atIndex:(price.length -(i+1)*3)]; } } return price1; }
11.ios 禁用多個按鈕同時按下的效果(解決bug)
把那些不能同時點下的按鈕或者視圖設置一下便可:
[view setExclusiveTouch:YES];
12.打亂數組元素的順序
-(void)Shuffle:(NSMutableArray*) arr { for (int i = 0; i < arr.count; ++i) { int n = (arc4random() % arr.count - i) + i; [arr exchangeObjectAtIndex:i withObjectAtIndex:n]; } }
能夠把它弄成數組的類目。