2014年10月29日 星期三

使用DataSource接外部資料提供自己的模組之用

模組平常都是用delegate去呼叫外部method來當作Callback使用,然而我們也可以利用delegate來作為DataSource去接外部資料回來使用。

//在.m中這樣呼叫你由外部輸入的Data
NSString *myKey = [self.dataSource getMyKey:self];

//在.h中這樣宣告
@protocol ReportChartDataSource;

@interface ReportChart : UIView

@property (weak, nonatomic) id<ReportChartDataSource> dataSource;

@property (nonatomic, readonly) ReportDuration duration;

- (void)reloadData;
- (void)reloadDataWithDuration:(ReportDuration)duration;

@end

//宣告實作
@protocol ReportChartDataSource <NSObject>

- (NSInteger) getMyKey:(ReportChart *)chart;

@end

NSMutableArray initWithCapacity 的用途

對於Mutable的物件,init不會配置足夠的記憶體,隨著內容物件的增加而重新配置記憶體,這會造成效能上的損失,為了解決這個問題,我們可以使用這個方法,在初始化時就先指定之後會用到的記憶體大小給你的Array,但不用擔心overflow因為如果使用超過了,仍然會幫你增加。
Returns an array, initialized with enough memory to initially hold a given number of objects.

Return Value

An array initialized with enough memory to hold numItems objects. The returned object might be different than the original receiver.

Discussion

Mutable arrays expand as needed; numItems simply establishes the object’s initial capacity.
This method is a designated initializer.

2014年10月28日 星期二

對NSArray中的ManagedObjects做排序

NSArray *recordMOArray = [self.petInfoMO.recordEntities allObjects];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray = [recordMOArray sortedArrayUsingDescriptors:sortDescriptors];
    
    NSDate* end = [NSDate date];

    NSDate* start = ((RecordMO *)[sortedArray firstObject]).date;

2014年10月17日 星期五

修改狀態列樣式

Go to Info tab of the project target, Add Row:

UIViewControllerBasedStatusBarAppearance, set value NO

Then in appdelegate.m

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

[application setStatusBarStyle:UIStatusBarStyleLightContent];
}

判別iOS是否是64位元機器(pre-compile的code)

#if __LP64__
    \\You're running on 64 bit
#else
    \\You're running on 32 bit
#endif

2014年10月9日 星期四

viewWillAppear在NavigationController下無法發揮作用

先宣告<UINavigationControllerDelegate>
然後再把NavigationController.delegate = self;

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isEqual:self]) {
            [viewController viewWillAppear:animated];
    } else if ([viewController conformsToProtocol:@protocol(UINavigationControllerDelegate)]){
            // Set the navigation controller delegate to the passed-in view controller and call the UINavigationViewControllerDelegate method on the new delegate.
            [navigationController setDelegate:(id<UINavigationControllerDelegate>)viewController];
            [[navigationController delegate] navigationController:navigationController willShowViewController:viewController animated:YES];
    }
}
- (void)navigationController:(UINavigationController *)navigationController 
       didShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated
{
    if ([viewController isEqual:self]) {
        [self viewDidAppear:animated];
    }
}

不錯的iOS開發網站

星期五Q&A
https://mikeash.com/pyblog/