关于IOS开发的一些有用的代码片段(别人总结和自己的)

发布时间:2020-07-18 18:21:22 作者:陈破虏
来源:网络 阅读:563


1.如何用NSLog输出NSRange,CGRect等结构体

NSString 中的方法:

NSStringFromCGPoint  
NSStringFromCGSize  
NSStringFromCGRect  
NSStringFromCGAffineTransform  
NSStringFromUIEdgeInsets


如:NSLog(@"rect1: %@", NSStringFromCGRect(rect1));


2.如何在navigationviewcontroller中,pop到之前不同的viewcontroller(push过的viewcontroller)?

每当我们push到一个viewcontroller时,就会把这个viewcontroller的实例保存到NSArray里,通过array可以获取到任何一个viewcontroller。

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之间进行传值?

简单点的来,两个界面间传值,直接上代码了:

sendViewcontroller.m

//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文本变化的事件)

1、实现UITextFieldDelegate协议;
2、实现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];
    }
}

可以把它弄成数组的类目。

推荐阅读:
  1. php的一些有用的自己写的方法
  2. iOS开发的一些小技巧

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios 代码片段

上一篇:myeclipse 10 基本设置

下一篇:一篇文章助你理解Python3中字符串编码问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》