2019年6月11日 星期二

obj-c 保護你的delegate method calling


@protocol SearchViewDelegate<NSObject>

@optional
- (void)searchTextFieldDidBeginEditing;
- (void)onSearchTextFieldValueChanged:(NSString *)text;
@end


@interface SearchView()


@end


@implementation SearchView
typedef struct {
    unsigned int searchTextFieldDidBeginEditing:1;
    unsigned int onSearchTextFieldValueChanged:1;
} DelegateRespondsTo;
DelegateRespondsTo delegateRespondsTo;

- (void)setDelegate:(id <SearchViewDelegate>)aDelegate {
    if (_delegate != aDelegate) {
        _delegate = aDelegate;
        
        delegateRespondsTo.searchTextFieldDidBeginEditing = [_delegate respondsToSelector:@selector(searchTextFieldDidBeginEditing)];
        delegateRespondsTo.onSearchTextFieldValueChanged = [_delegate respondsToSelector:@selector(onSearchTextFieldValueChanged:)];
    }
}

2019年5月7日 星期二

textFieldShouldClear dismiss keyboard

func textFieldShouldClear(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()
        
    textField.text = nil
        
    return false
}

2019年4月8日 星期一

改變UISegmentedControl的font

#import "UISegmentedControl+Font.h"

@implementation UISegmentedControl(Font)

- (void)setFont:(UIFont *)font color:(UIColor *)color {
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil];
    [self setTitleTextAttributes:attributes forState:UIControlStateNormal];
    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected];
}

@end

2018年12月14日 星期五

better way 更新tableview

當你一個小改變要更新cell的時候,reloadData 會導致很大的效能浪費,更慘的是tableview會有不必要的動畫被看到。

有一種解法是放delegate去給tableviewcell執行,這樣不用重繪tableview的整個cell,但還是會給所有的cell執行一次。

更好的作法是先更新tableview data,但不去reloadData,然後只更新看得見的部份
for (EmployeeListTableViewCell *cell in self.tableView.visibleCells) {
            //TODO: update the UI
}
剩下的交給tableView自己,在滑動到其他cell的時候更新。

2018年11月6日 星期二

UIApplicationWillChangeStatusBarOrientationNotification v.s. UIApplicationDidChangeStatusBarOrientationNotification


結論:
UIApplicationWillChangeStatusBarOrientationNotification
會先被呼叫,而且userInfo 是傳送將要轉換的 orientation。

UIApplicationDidChangeStatusBarOrientationNotification
後被呼叫,但userInfo是傳送轉換前的 orientation。

code:

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationWillChange:)
                                               name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
   
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationDidChange:)
                                               name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusOrientationWillChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
    [self.tableView reloadData];
}

- (void)statusOrientationDidChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
}

Strange thing: A - 2 > 0 is not equvalent to A > 2

(lldb) po self.customFields.count - 2 > 0
true

(lldb) po self.customFields.count > 2
false

(lldb) po self.customFields
<__NSArrayM 0x108e54540>(

)

(lldb) po self.customFields.count
<nil>