编程技巧 - 7

来源:互联网 发布:淘宝帐号能注销吗 编辑:程序博客网 时间:2024/06/02 03:56

40.让常数变得更优雅!

+ (CGFloat)textViewLineHeight{    return 36.0f; // for fontSize 16.0f}+ (CGFloat)maxLines{    return 4.0f;}+ (CGFloat)maxHeight{    return ([JSMessageInputView maxLines] + 1.0f) * [JSMessageInputView textViewLineHeight];}

    CGFloat maxHeight = [JSMessageInputView maxHeight];




41.服务器的返回解析也要分好类

分好类,健壮性更好!

同理的:

+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view{    //参数也可能是空的,如果是空的,则“就地取材”    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];        
这里也是同样的原理,如果传来的view是一个空值,则取当前的对象。



42.弄个plist文件来存储参数:




43.通知要这样写才好!

        [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(handleKeyboardWillShowHideNotification:)                                                     name:UIKeyboardWillShowNotification                                                   object:nil];                [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(handleKeyboardWillShowHideNotification:)                                                     name:UIKeyboardDidShowNotification                                                   object:nil];                [[NSNotificationCenter defaultCenter] addObserver:self                                                 selector:@selector(handleKeyboardWillShowHideNotification:)                                                     name:UIKeyboardDidHideNotification                                                   object:nil];


通过name来分类:

#pragma mark - Notifications- (void)handleKeyboardWillShowHideNotification:(NSNotification *)notification{    if([notification.name isEqualToString:UIKeyboardWillShowNotification]) {        self.keyboardView.hidden = NO;    }    else if([notification.name isEqualToString:UIKeyboardDidShowNotification]) {        self.keyboardView = self.inputAccessoryView.superview;        self.keyboardView.hidden = NO;                if(self.keyboardDelegate && [self.keyboardDelegate respondsToSelector:@selector(keyboardDidShow)])            [self.keyboardDelegate keyboardDidShow];    }    else if([notification.name isEqualToString:UIKeyboardDidHideNotification]) {        self.keyboardView.hidden = NO;        [self resignFirstResponder];    }}




44.嵌套:

只在.m文件写这个方法:

- (id)initWithObject:(id)object keyPath:(NSString*)keyPath target:(id)target selector:(SEL)selector options:(NSKeyValueObservingOptions)options;{    if (object == nil) {        return nil;    }    NSParameterAssert(target != nil);    NSParameterAssert([target respondsToSelector:selector]);    self = [super init];    if (self) {        self.target = target;        self.selector = selector;        self.observedObject = object;        self.keyPath = keyPath;        [object addObserver:self forKeyPath:keyPath options:options context:(__bridge void *)(self)];    }    return self;}


在.m文件嵌套(使用封装):

+ (NSObject *)observeObject:(id)object keyPath:(NSString*)keyPath target:(id)target selector:(SEL)selector options:(NSKeyValueObservingOptions)options __attribute__((warn_unused_result));{    return [[self alloc] initWithObject:object keyPath:keyPath target:target selector:selector options:options];}



45.setFrame 指定Frame的origin位置:

    containerViewRect.origin.x = round(CGRectGetMidX(self.window.bounds)-CGRectGetMidX(containerViewRect));    containerViewRect.origin.y = round(CGRectGetMidY(self.window.bounds)-CGRectGetMidY(containerViewRect));

这中写法非常好看!


0 0