Iphone代碼片段

 Iphone代碼片段導航

 Iphone開發代碼片段1

Iphone開發代碼片段2

 Iphone開發代碼片段3 

1.給UITableViewController添加ToolBar。

 self.navigationController.toolbarHidden = NO; //默認是隱藏的。

//添加MessageToolBar ,messageToolBar是IBOutlet的一個ToolBar。

 self.toolbarItems =  [[[NSMutableArray alloc] initWithArray:self.messageToolBar.items] autorelease];

 self.navigationController.toolbar.barStyle = self.messageToolBar.barStyle; 

2.後臺運行一個方法,如果該方法需要修改UI,爲了防止出錯,應在主線程裏修改UI。

[self performSelectorInBackground:@selector(updateInfo)];

在UpdateInfo裏如果要修改UI ,

[self performSelectorOnMainThread:@selector(updateUIMethod) withObject:nil waitUntilDone:NO];

同時注意,後臺程序的方法應該放在NSAutoRelease pool裏的,如下所示:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
xxxx
[pool release];

3.在A類裏動態的設定B類或者C類的方法。

[self.actionTarget performSelector:self.actionMethod withObject:parameter];

actionTarget   -> id類型的屬性。設置B 類或者C類。

actionMethod -> Sel類型的屬性。設置具體的方法名

parameter     -> 參數

4.設置Navigation的提示信息和進度條設置

   self.navigationItem.prompt : 提示信息
   self.navigationItem.titleView :存放ProgressBar等其它提示信息的View

   在進度條顯示完了後,需要清空顯示進度信息:

   self.navigationItem.prompt = nil;
   self.navigationItem.titleView = nil;

5.從資源文件xib里加載View的方法

 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyView"
                                                         owner:self
                                                       options:nil];
MyView *view = [nib objectAtIndex:0];

6. UIAlterView 修改默認的Frame高度

在其委託裏實現這個方法

- (void)willPresentAlertView:(UIAlertView *)alertView 

{

    alertView.frame = CGRectMake(5.f, 1.f, 100.f, 200.f);

}

參考:http://stackoverflow.com/questions/2763713/change-width-of-uialertview-in-ipad

 

 7.獲取iphone屏幕大小

CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];
CGRect screenRect= [ [ UIScreen mainScreen ] applicationframe ]; 

8. 修改TableView的樣式,讓UITableView顯示Windows的背景圖片。

    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.opaque = NO;
    self.tableView.backgroundView = nil;

   如果要修改UITableCell的事情backgroundColor需要再 tableView:willDisplayCell:forRowAtIndexPath:裏修改。

9.通過圖片獲取顏色。 

[UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName"]];

修改分割線顏色

 self.tableView.separatorColor = [UIColor blackColor]; 

顯示文本的地方設置透明色 

 cell.textLabel.opaque = NO;

 這樣整個cell就有立體感。 

10.設置UITableView 的checkmark顯示樣式

修改cell的 accessoryView 

 cell.accessoryView = UIImageView

11. 修改TableView距離導航纜的高度。 

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{
    return 10.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];;
}

12. 自定義TableViewCell的背景顏色和選擇後的顏色。

 方法一:將TableViewCell的backgroundView和SelectBackGroundView修改成指定的View就可以了。

 方法二: 在Interface Builder裏設置cell的image和SelectImage屬性,但是要記得UItableView修改seperator的屬性爲None

13 顏色定義。

  美工一般定義好顏色,然後讓程序員去填充顏色,美工一般給的是RGB顏色,那麼RGB顏色如果換成UIColor

[UIColor colorWithRed:31.0/255 green:204.0/255 blue:39.0/255 alpha:1.0];

Red,Green,Blue只接受0-1的參數,換算方法是除以255。 

14. Xcode 4設置  NSZombieEnabled

 if you click on the scheme drop down bar -> edit scheme -> arguments tab and then add NSZombieEnabled in the Environment Variables column and YES in the value column

15.自動生成多語言化的StringTable

   如果在代碼裏全部是通過 NSLocalizedString(@"中文", nil)來對應多語言,最後要整理一個list,手動一個一個粘貼太麻煩。

  自動化生成方法:在命令行目錄下進入項目根目錄:執行 genstrings -a $(find . -name "*.m"),就會自動生成一個文件對應。

  參考網址 http://steelwheels.sourceforge.jp/Documents/genstring.html

http://iphone.longearth.net/2009/05/25/%E3%80%90iphone%E3%80%91localizablestrings%E3%82%92%E8%87%AA%E5%8B%95%E3%81%A7%E4%BD%9C%E3%82%8B-genstrings/ 

16.自定義bond字體 

[UIFont fontWithName:@"Helvetica-Bold" size:16.0] 

17  無邊框透明UITableViewCell

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;   

self.tableView.separatorColor = [UIColor clearColor];              

self.tableView.backgroundColor = [UIColor clearColor];   

self.tableView.opaque = NO;   

self.tableView.backgroundView = nil;

--Cell修改--

self.backgroundView = [[[UIView alloc] init] autorelease];       

self.backgroundView.backgroundColor = [UIColor clearColor];       

self.selectedBackgroundView = [[[UIView alloc] init] autorelease];       

self.selectedBackgroundView.backgroundColor = [UIColor clearColor];

18. 隱藏Tabbar

SampleViewController *obj = [[SampleViewController alloc] init];

[obj setHidesBottomBarWhenPushed:YES];

[self.navigationController pushViewController:obj animated:YES];

[obj release];
19.從UIView獲取UImage

#import QuartzCore/QuartzCore.h

- (UIImage *)getImageFromView:(UIView *)orgView  

{ UIGraphicsBeginImageContext(orgView.bounds.size);  

[orgView.layer renderInContext:UIGraphicsGetCurrentContext()];  

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  

UIGraphicsEndImageContext();  

return image;  

20. 添加手式識別後,會屏蔽掉touchend方法

 

21.獲取手機號碼,和IMEI  

 

獲取本地iphone手機號碼

[[NSUserDefaults standardUserDefaults] valueForKey:@"SBFormattedPhoneNumber"];  

獲取手機的imei

#import "Message/NetworkController.h" 

NetworkController *ntc=[[NetworkController sharedInstance] autorelease];  

NSString *imeistring = [ntc IMEI];  

imeistring就是獲取的imei。 IMEI(International Mobile Equipment Identity)是國際移動設備身份碼的縮寫,國際移動裝備辨識碼,是由15位數字組成的"電子串號",它與每臺手機一一對應,而且該碼是全世界唯一的。

22 NLog的格式,經常忘記,做個筆記

%@ 對象
%d, %i 整數
%u   無符整形
%f 浮點/雙字
%x, %X 二進制整數
%o 八進制整數
%zu size_t
%p 指針
%e   浮點/雙字 (科學計算)
%g   浮點/雙字
%s C 字符串
%.*s Pascal字符串
%c 字符
%C unichar
%lld 64位長整數(long long)
%llu   無符64位長整數
%Lf 64位雙字
 

23.更改UISearchBar最下面黑色的邊框

 #define SEARCHBAR_BORDER_TAG 1337

- (void) viewDidLoad{
    // Set a custom border on the bottom of the search bar, so it's not so harsh
    UISearchBar *searchBar = self.searchDisplayController.searchBar;
    UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0,searchBar.frame.size.height-1,searchBar.frame.size.width, 1)];
    [bottomBorder setBackgroundColor:[UIColor colorWithWhite:200.0f/255.f alpha:1.0f]];
    [bottomBorder setOpaque:YES];
    [bottomBorder setTag:SEARCHBAR_BORDER_TAG];
    [searchBar addSubview:bottomBorder];
    [bottomBorder release];
}

 

24.設置鍵盤的默認形式。

   比如UITextField 設置爲默認數字,和只允許數組數字

   //默認數字 

   textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation
   //只允許輸入數字   

   textField.keyboardType = UIKeyboardTypeNumberPad

 

25.UIButton設置文字左對齊

 - emailBtn.contentHorizontalAlignment = UIontrolContentHorizontalAlignmentLeft;
 - emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
 - CGRect buttonRect = emailBtn.bounds;  
   UILabel *myLabel = [[UILabel alloc] initWithFrame: buttonRect];
   myLabel = UITextAlignmentLeft;
   [emailBtn addSubview:myLabel];

   [myLabel release];  

26. retain異常的時候重載這個方法設置斷點查看和分析

- (id) retain

{
    // Break here to see who is retaining me.
    return [super retain];
}

 

27.去掉白色半圓

Plist添加 

 Icon already includes gloss effects 爲YES

UIPrerenderedIcon 設置不起作用(Xcode4 .0.2)
28.tableView reloadRowsAtIndexPaths 如果不在可見區域,將不會重新加載。
 

29. 設置應用程序的statusbaryanse

再plist裏設置Status bar style  Opaque black style

 

30. 設置控件的copy paste的本地化

   - 設置Localization native development region   =》 china

   - 將項目的en.lproj 改成zh_CN.lproj

 31. 允許應用程序通過itunes上傳文件(ios3.2以上)

 在info.plist裏設置 UIFileSharingEnabled  => YES

 32. 獲取UICOLOR的rgb值

const CGFloat* components = CGColorGetComponents(SelectedColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]); 
NSLog(@"Blue: %f", components[2]);

NSLog(@"Alpha: %f", CGColorGetAlpha(SelectedColor.CGColor)); 

 

33.獲取2個時間之間的天,小時,分鐘

 +(NSString *)TimeRemainingUntilDate:(NSDate *)date {

    NSTimeInterval interval = [date timeIntervalSinceNow];
    NSString * timeRemaining = nil;
    if (interval > 0) {
        div_t d = div(interval, 86400);
        int day = d.quot;
        div_t h = div(d.rem, 3600);
        int hour = h.quot;
        div_t m = div(h.rem, 60);
        int min = m.quot;
        NSString * nbday = nil;
        if(day > 1)
            nbday = @"days";
        else if(day == 1)
            nbday = @"day";
        else
            nbday = @"";
        NSString * nbhour = nil;
        if(hour > 1)
            nbhour = @"hours";
        else if (hour == 1)
            nbhour = @"hour";
        else
            nbhour = @"";
        NSString * nbmin = nil;
        if(min > 1)
            nbmin = @"mins";
        else
            nbmin = @"min";
        timeRemaining = [NSString stringWithFormat:@"%@%@ %@%@ %@%@",day ? [NSNumber numberWithInt:day] : @"",nbday,hour ? [NSNumber numberWithInt:hour] : @"",nbhour,min ? [NSNumber numberWithInt:min] : @"00",nbmin];
    }
    else
        timeRemaining = @"Over";
    return timeRemaining;
}

 

34. Icon specified in the Info.plist not found under the top level app wrapper 

     記住Icon 首字母是大寫的,不是icon.png , 是Icon.png 

 

35. 

[iphone]Code Sign error: Provisioning profile XXXX can't be found

http://www.cnblogs.com/baryon/archive/2010/05/06/1728968.html 

http://www.douban.com/note/131009422/ 

 

   1.關閉你的項目,找到項目文件XXXX.xcodeproj,在文件上點擊右鍵,選擇“顯示包內容”(Show Package Contents)。會新打開一個Finder。注:其實XXXX.xcodeproj就是一個文件夾,這裏新打開的一個Finder裏面的三個文件就是該XXXX.xcodeproj文件夾裏面的文件。
2.在新打開的Finder中找到project.pbxproj,並且打開。在這之中找到你之前的證書的編碼信息。我之前報的錯誤信息是
Code Sign error: Provisioning profile '37D44E7F-0339-4277-9A82-C146A944CD46',所以我用查找的方式找到了所有包括37D44E7F-0339-4277-9A82-C146A944CD46的行,並且刪除。
 3.保存,重新啓動你的項目,再編譯。就OK了。

 

 36.獲取手機唯一ID

UIDevice *device = [UIDevice currentDevice];//創建設備對象
NSString *deviceUID = [[NSString alloc] initWithString:[device uniqueIdentifier]];

NSLog(@"%@",deviceUID); // 輸出設備id 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章