编程技巧 - 9

来源:互联网 发布:linux中rpm文件 编辑:程序博客网 时间:2024/06/06 16:29

1.限制长度

限制TextField长度,大把方法啦,什么直接if判断字符长度,正则什么的,现在介绍一个我觉得比较好的

- (void)textFieldDidChange:(UITextField *)textField {    if (textField.text.length > 20) {        textField.text = [textField.text substringToIndex:20];        [[SYTipView shareInstance] showErrorTipWithText:@"长度不能超过20字符"];    }}

用委托方法里面的限制,截取前20的时候就给他提示一次

还有这样的技巧,用一个变量将当前的字符内容存着:

- (void)textFieldDidBeginEditing:(UITextField *)textField{        _currentTextField = textField;}


为什么要这么执着限制长度咧??因为安全啊!

1.字符太长,甚至无限长,请求长度会过长导致服务器无法处理

2.担心恶意注入代码,一些让服务器跨掉的代码,我们都知道请求是拼接起来的,要是拼接了有问题的代码就坑爹了





2.遍历完,你还要用,怎么办!

我们时常在for循环中,顺便把遍历因子写在括号作用域中,其实可以这样:

        int index;                for (index = 0; index < _tags.count; index ++)

这样一个遍历完成后,这个index你还可以拿来用!





3.将CGRect CGSize 这些东西存起来!怎么存?

因此我们用NSStringFromCGRect()方法将item的位置信息转换成字符串后和indexPath成对存储在字典中

这些编程技巧非常实用





4.炫酷的自定义navigationBar

无意中看到一个非常棒的自定义navigationBar的方式:

自定义的navigationBar:

#import <UIKit/UIKit.h>@interface YALNavigationBar : UINavigationBar@end


重写其中的sizeThatFits:

static float const defaultHeight = 65.f;#import "YALNavigationBar.h"@implementation YALNavigationBar- (CGSize)sizeThatFits:(CGSize)size {        CGSize amendedSize = [super sizeThatFits:size];    amendedSize.height = defaultHeight;        return amendedSize;}@end



用KVC的形式设置其中的样式:

    // set custom navigationBar with a bigger height    [self.navigationController setValue:[[YALNavigationBar alloc]init] forKeyPath:@"navigationBar"];


这样,Navigation的Bar就会变大了!炫酷吧!


0 0