2011年5月26日 星期四

Objective C 小技巧 ﹣ 由Object獲取Class的所有Method名

今天做實驗,
想試出有什麼方法可以更改iOS SDK中一些私有API,
而又可以理論上通過App Store的審核。
不過作為私有API,我們根本就無法看得到Class的Header File,
哪麼最簡單更改方法就只好由Method入手,
因為Objective C有Categories的Override功能,
十分好用,可作某Method下的覆寫。

代碼如下
// 必須的Header file
#import <objc/runtime.h>

// 找Method名的代碼
unsigned int methodCnt = 0;
Method* methodList = class_copyMethodList([obj class], &methodCnt);
for (int i = 0; i < methodCnt; i++)
{
   Method method = methodList[i];
   // Print Method Name
   NSLog(@"%@", NSStringFromSelector(method_getName(method)));
}

2011年5月24日 星期二

iOS UIWindow 小技巧﹣令UIViewController收不到Interface Orientation Callback的原因

今天開始接手同事的iPhone項目,
我主要的工作是把項目轉為同時支援iPad的版本

剛開始就發現項目不論在iPhone或是iPad都不支持Interface Orientation,
經過同事和我的努力,
即使在plist和UIViewController的Interface Orientation Callback,
設定了都不可以收到Interface Orientation

最後同事發現到原來是Interface Builder,
把viewController的view加到UIWindow中就會引發這個問題,
他之後改為programme code去把viewController的view加到UIWindow中就沒事了
// 把viewController的view加到UIWindow中
[self.window addSubview:viewController.view];

2011年5月22日 星期日

MySQL﹣一行SQL可以把別的Table內容加到當前Table中

今天錯手刪了公司DB中一個重要得Table內容,
我即時向上司回報,
幸好哪Table是Match用的Table,
上司即時教我把Data由別的Tablel中Recove,
真是有驚無險

INSERT INTO MatchTable ( id, index )
SELECT  OtherTable.id, OtherTable.index
FROM OtherTable
WHERE state=1;

2011年5月6日 星期五

iOS UITableView 小投巧(二)﹣修改Section Header方便方法

一般大家修改Section Header都會用
// 修改Header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
// 修改Header介面的View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

但是今天我發現可以利用UITableView本身的property tableHeaderView
不過它只會出現在UITableView的最上方,
因為它根不就不是一個Section Header,
只是在UITableView用到一Section Header,
就可以用它來簡化代碼
UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 245, 50)];
[titleLabel setText:@"Title"];
[titleLabel setFont:[UIFont boldSystemFontOfSize:22]];
//[titleLabel setBaselineAdjustment:UIBaselineAdjustmentAlignCenters];
[titleLabel setTextAlignment:UITextAlignmentCenter];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
self.tableView.tableHeaderView = titleLabel;
[titleLabel release];

iOS UITableView 小投巧(一)﹣優化效能

最近一位同事要利用UITableView載入二萬個Record
不過UITableView用了約兩分鐘才完成初始化
真是慢得無朋友

最後我們發現他原來用了UITableView Delegate Callback的heightForHeaderInSection Function
去修改Record的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 100;
}

我們都覺得是Function Call太多的關係
是令UITableView慢原因
最好的方法都是用UITableView的property rowHeight去修改
_tableView.rowHeight = 100;

iPhone Three20 Framework中修改TTTabBar顏色的方法

今天小弟第一次使用Three20 Framework被它的Demo UI吸引,
所以在工作中用上了,
發覺TTTabBar沒有setBackgroundColor這類的Function去修改TabBar的介面顏色
後來我發現了,
原來大部的UI所用的介面顏色都是由TTDefaultStyleSheet Class所控制
所以我利用了Objective C的Categories特性,
修改了TTDefaultStyleSheet Class控制TabBar顏色部份

代碼如下
@implementation TTDefaultStyleSheet (CategoriesOverride)

- (TTStyle*)tabBar
{
   UIColor* border = [TTSTYLEVAR(tabBarTintColor) multiplyHue:0 saturation:0 value:0.7];

      return
         [TTSolidFillStyle styleWithColor:[UIColor blackColor] next:
            [TTFourBorderStyle styleWithTop:nil right:nil bottom:border left:nil width:1 next:nil]];
}

@end

P.S. 粗體黃字為修改的背景色

2011年5月3日 星期二

自定義UIPageControl的介面

今天工作時發現要修改UIPageControl的介面,
不過上Google找不到想要的答案,
只好自己試出來了XD

其實修改2個Function就可以做到
- (void)setNumberOfPages:(NSInteger)pages;
- (void)setCurrentPage:(NSInteger)page;

- (void)setNumberOfPages:(NSInteger)pages
{
// super class function
[super setNumberOfPages:pages];

// search all page image view
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* imageView = [self.subviews objectAtIndex:i];
[imageView setFrame:CGRectMake(0, 0, 25, 25)];
// if image view no subview
if([imageView.subviews count] == 0)
{
// add a new subview cover image view
UILabel* numLabel = [[UILabel alloc] initWithFrame:imageView.frame];
[numLabel setText:[NSString stringWithFormat:@"%d", i + 1]];
[numLabel setBackgroundColor:[UIColor blackColor]];
[numLabel setTextAlignment:UITextAlignmentCenter];
[numLabel setTextColor:[UIColor whiteColor]];
[imageView addSubview:numLabel];
[numLabel release];
}

// edit the page image view each position
[imageView setCenter:CGPointMake(40 * i, self.frame.size.height / 2)];
imageView.layer.borderWidth = 2;
imageView.layer.borderColor = [UIColor grayColor].CGColor;
imageView.layer.cornerRadius = 8;
imageView.layer.masksToBounds = TRUE;
}
}

- (void)setCurrentPage:(NSInteger)page
{
// super class function
[super setCurrentPage:page];

// clean all image view select state
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView* imageView = [self.subviews objectAtIndex:i];
UILabel* numLabel = [imageView.subviews objectAtIndex:0];
[numLabel setBackgroundColor:[UIColor blackColor]];
[numLabel setTextColor:[UIColor whiteColor]];
}

// update the current select page image view state
UIImageView* imageView = [self.subviews objectAtIndex:page];
UILabel* numLabel = [imageView.subviews objectAtIndex:0];
[numLabel setBackgroundColor:[UIColor whiteColor]];
[numLabel setTextColor:[UIColor blackColor]];
}