ios開發之部分問題總結

iphone開發 NavBar+TarBar
1  改變NavBar顏色:選中Navigation Bar 的Tint屬性。選中顏色。

2  隱藏“back”按鈕: self.navigationItem.hidesBackButton = YES;

3 隱藏"NavBar" : self.navigationController.navigationBarHidden = YES;

4 可以不用MainWindow.xib創建的Navigation。在每個view上自定義。

需要把每個控制頁都加上以下代碼來隱藏nav:
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;//顯示"NavBar"
}
然後在每個控制頁xib自己添加Navigation Bar。添加所需BarButtonItem按鈕。


5 頁面跳轉隱藏tarbar :
HomeDetailViewController *detailview = [[HomeDetailViewController alloc] initWithNibName:@"HomeDetailView" bundle:nil];
detailview.hidesBottomBarWhenPushed = YES;//隱藏tarbar 
[self.navigationController pushViewController:detailview animated:YES];
[detailview     release];

6 頁面返回:
[self.navigationController popViewControllerAnimated:YES];

7 默認選中tabbar爲第一個view:
TabBarController.selectedIndex= 0;


其他:
8 已知兩地經緯度 計算兩地之間的距離:
//    地圖顯示當前位置:
    mapView.showsUserLocation=YES;
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];//創建位置管理器
    locationManager.delegate=self;//設置代理
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度級別爲最佳精度
    locationManager.distanceFilter=1000.0f;//設置距離篩選器爲任何移動都要發送更新
    [locationManager startUpdatingLocation];//啓動位置管理器
    MKCoordinateSpan theSpan;
    //地圖的範圍 越小越精確
    theSpan.latitudeDelta=0.05;
    theSpan.longitudeDelta=0.05;
    MKCoordinateRegion theRegion;
    theRegion.center=[[locationManager location] coordinate];
    theRegion.span=theSpan;
    [mapView setRegion:theRegion];
    [locationManager release];
    
    MKUserLocation *usrLoc=mapView.userLocation;
    CLLocationCoordinate2D usrCoordinate=usrLoc.location.coordinate;
    NSLog(@"la==%f lo==%f",usrCoordinate.latitude,usrCoordinate.longitude);
    
//   已知兩點的經緯度,計算出兩地距離:
    CLLocation *location1 = [[[CLLocation alloc] initWithLatitude:usrCoordinate.latitude longitude:usrCoordinate.longitude] autorelease];
    CLLocation *location2 = [[[CLLocation alloc] initWithLatitude:36.676445 longitude:117.106793] autorelease];
    NSLog(@"JULI====%.0f km", [location1 distanceFromLocation:location2]);//4502


9 取小數點後兩位(四捨五入),輸出:
NSLog(@"%.02f km",4478.442312); 

10 調用打電話API :
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];
使用這種方式撥打電話時,當用戶結束通話後,iphone界面會停留在電話界面。
用如下方式,可以使得用戶結束通話後自動返回到應用:
UIWebView*callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:@"tel:10086"];// 貌似tel:// 或者 tel: 都行
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    //記得添加到view上
    [self.view addSubview:callWebview];

11 調用 SMS發短信:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://15315310992"]];

12 調用自帶 瀏覽器 safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];

13 在一個程序裏打開另一個程序:
首先:plist裏添加URL types   點開裏邊的Item0  添加URLSchemes  打開Item0 輸入sinaWeibo
然後在需要調用的地方:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sinaWeibo://*"]];

https://itunes.apple.com/cn/app/qq-2012/id444934666?mt=8

14 雙引號轉義:
用 \" 表示 雙引號

15 設置按鈕按下換圖片 鬆開還是原圖
//                [danxuan setImage:[UIImage imageNamed:@"exercise_option_n.png"] forState:UIControlStateNormal];
                //這個是設置按下的圖片,鬆開就是上面的圖片
//               [danxuan setImage:[UIImage imageNamed:@"exercise_option_s.png"] forState:UIControlEventTouchDragOutside];

16 UIAlertView有3個按鈕,直接在other裏直接加,以逗號隔開
UIAlertView *alerDuiBiViewAll = [[UIAlertView alloc] initWithTitle:@"是否加入對比?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"加入對比",@"刪除本項",nil];
[alerDuiBiViewAll show]; 
//獲取alertView的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex ==1)//對比:
 {
}
if(buttonIndex ==2)//刪除:
 {
}
}

數組相關:
17  判斷數組中是否存在某元素:
BOOL isValue=[keyArray containsObject:@"aaa"];

18.1把字符串按逗號隔開,並保存到數組:
NSArray *keyArray=[[NSArray alloc] init];
keyArray=[@"冬瓜,西瓜,南瓜,苦瓜,絲瓜" componentsSeparatedByString:@","];

18.2把數組中的取出來,拼成用逗號隔開的字符串:
NSString *n=[keyArray componentsJoinedByString:@","];

18.3 NSMutableArray轉化成NSArray
NSArray *phoneA=[[NSArray alloc] init];
NSMutableArray *phoneArrayss=[[NSMutableArray alloc] init];
phoneA=[phoneArrayss mutableCopy];


19獲取本地時間(大寫HH獲取24小時制的)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *timeStr=[formatter stringFromDate: [NSDate date]];

20 UITableView一些屬性:
tableView.bounces=NO//禁止拖動
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉邊框
[cellView setSelectionStyle:UITableViewCellSelectionStyleNone];//禁止觸發點擊某行
//實現某行的選中效果(點擊某行變顏色,鬆開還是剛變的顏色,點擊其他行這行顏色消失):
[cellView setBackgroundColor:[UIColor clearColor]];    
    cellView.selectedBackgroundView = [[[UIView alloc] initWithFrame:cellView.frame] autorelease];
    UIImageView *ia1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)];
    ia1.image=[UIImage imageNamed:@"ts_bg01.png"];
    cellView.backgroundView=ia1;
    
    UIImageView *ia2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 170)];
    ia2.image=[UIImage imageNamed:@"ts_bg02.png"];
    cellView.selectedBackgroundView=ia2;


21 通知NSNotificationCenter相關:
21.1定義通知:
NSNotificationCenter *ncn = [NSNotificationCenter defaultCenter];
[ncn addObserver:self selector:@selector(DuiBiShow:) name:@"DuiBiButton" object:nil];

21.2定義通知調用的方法:
- (void)DuiBiShow:(NSNotification*) notification
{
NSMutableArray *Array = [notification object];//通過這個獲取到傳遞的對象
}

21.3調用通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"DuiBiButton" object:passDuiBiArray];

21.4 移除name爲DuiBiButton的通知:
  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"DuiBiButton" object:nil];

21.5 移除本ViewControll頁的所以通知:
[[NSNotificationCenter defaultCenter] removeObserver:self];

22 UIScrollView屬性:
tracking //當 touch 後還沒有拖動的時候值是YES,否則NO
zoomBouncing //當內容放大到最大或者最小的時候值是 YES,否則 NO
zooming //當正在縮放的時候值是 YES,否則 NO
decelerating //當滾動後,手指放開但是還在繼續滾動中。這個時候是 YES,其它時候是 NO
decelerationRate //設置手指放開後的減速率
maximumZoomScale //一個浮點數,表示能放最大的倍數
minimumZoomScale  //一個浮點數,表示能縮最小的倍數
pagingEnabled //當值是 YES 會自動滾動到 subview 的邊界。默認是NO
scrollEnabled //決定是否可以滾動
sView.contentSize = CGSizeMake(320*5,372);//滾動的範圍

delaysContentTouches //是個布爾值,當值是 YES 的時候,用戶觸碰開始,scroll view要延遲一會,看看是否用戶有意圖滾動。假如滾動了,那麼捕捉 touch-down 事件,否則就不捕捉。假如值是NO,當用戶觸碰, scroll view 會立即觸發 touchesShouldBegin:withEvent:inContentView:,默認是 YES

canCancelContentTouches //當值是 YES 的時候,用戶觸碰後,然後在一定時間內沒有移動,scrollView 發送 tracking events,然後用戶移動手指足夠長度觸發滾動事件,這個時候,scrollView 發送了 touchesCancelled:withEvent: 到 subview,然後 scroView 開始滾動。假如值是 NO,scrollView 發送 tracking events 後,就算用戶移動手指,scrollView 也不會滾動。

contentSize //裏面內容的大小,也就是可以滾動的大小,默認是0,沒有滾動效果。
showsHorizontalScrollIndicator //滾動時是否顯示水平滾動條
showsVerticalScrollIndicator //滾動時是否顯示垂直滾動條
bounces //默認是 yes,就是滾動超過邊界會反彈有反彈回來的效果。假如是 NO,那麼滾動到達邊界會立刻停止。
bouncesZoom //和 bounces 類似,區別在於:這個效果反映在縮放上面,假如縮放超過最大縮放,那麼會反彈效果;假如是 NO,則到達最大或者最小的時候立即停止。

directionalLockEnabled //默認是 NO,可以在垂直和水平方向同時運動。當值是 YES 時,假如一開始是垂直或者是水平運動,那麼接下來會鎖定另外一個方向的滾動。 假如一開始是對角方向滾動,則不會禁止某個方向
indicatorStyle //滾動條的樣式,基本只是設置顏色。總共3個顏色:默認、黑、白
scrollIndicatorInsets //設置滾動條的位置

23 UILabel自適應:
UILabel *requestlabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleTypelabel.frame.origin.y+titleTypelabel.frame.size.height+20, 280, 20)]; 
        UIFont *font2 = [UIFont systemFontOfSize:14];  
        [requestlabel setFont:font2];  
        [requestlabel setNumberOfLines:0];  
        [requestlabel setBackgroundColor:[UIColor clearColor]];      
        NSString *text2 = [NSString stringWithFormat:@"要求: %@",req];
        CGSize size2 = [text2 sizeWithFont:font2 constrainedToSize:CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];  
        CGRect rect2 = requestlabel.frame;  
        rect2.size = size2;  
        [requestlabel setFrame:rect2];  
        [requestlabel setText:text2];  
        [littleScrollView addSubview:requestlabel];

24 數據處理時 彈出的UIAlertView,上面放一個轉動的
UIActivityIndicatorView。當處理完數據UIAlertView自動消失。
loginAlerView = [[UIAlertView alloc]
                             initWithTitle:nil message:@"數據下載中\n請耐心等待⋯⋯\n\n"
                             delegate:nil cancelButtonTitle:nil
                             otherButtonTitles: nil];
            UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(120, 70, 40, 40)];
            [progressInd startAnimating];
            progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
            [loginAlerView addSubview:progressInd];
            [loginAlerView show];

//UIAlertView自動消失(和點擊完取消按鈕一樣的效果,在需要的時候調用):
[loginAlerView dismissWithClickedButtonIndex:[loginAlerView cancelButtonIndex] animated:YES];

25 去除nsstring中的空格 
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet ]; 
NSString * username = [mUsernameField stringValue]; 
username = [username stringByTrimmingCharactersInSet :whitespace];

26 實現動畫:一個接一個地顯示一系列的靜態圖象: 
NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.png"], [UIImage imageNamed:@"myImage2.png"], [UIImage imageNamed:@"myImage3.png"], [UIImage imageNamed:@"myImage4.gif"], nil]; 
UIImageView *myAnimatedView = [UIImageView alloc]; 
[myAnimatedView initWithFrame:[self bounds]]; 
myAnimatedView.animationImages = myImages; //animationImages屬性返回一個存放動畫圖片的數組 
myAnimatedView.animationDuration = 0.25; //瀏覽整個圖片一次所用的時間 
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 動畫重複次數 
[myAnimatedView startAnimating]; 
[self addSubview:myAnimatedView]; 
[myAnimatedView release];

27  UIWebView完整的顯示Html:
定義全局變量UIWebView *content;  加入代理UIWebViewDelegate;
//實例化:
content = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, 570, 540)];
    self.content.delegate=self;
    [self.content setUserInteractionEnabled:NO]; // 去掉滾動條並禁止滾動
    [content setBackgroundColor:[UIColor clearColor]];
    [self.noticeScroll addSubview:content];

NSString *htmlId = [NSString stringWithFormat:@"<div id='foo'>%@</div>",caseInfos.Content];//要在你的html裏面加foo標記用來計算html的長度。 
    [self.content loadHTMLString:[NSString stringWithFormat:htmlId] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

//當webView加載完成後調用:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //    得到html的高度:
    NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    [self.content setFrame:CGRectMake(0,60, 570,[output intValue]+30)];//設置UIWebView
    self.noticeScroll.contentSize = CGSizeMake(570, content.frame.origin.y+content.frame.size.height);//設置UIScrollView的滾動範圍
}

28 判斷是否有網絡
導入附件中的文件,然後在需要判斷的頁面引入#import "IsNetworking.h";
 IsNet.zip (2 K) 下載次數:35 
需要判斷的地方:
IsNetworking *IsNetWork = [[IsNetworking alloc] init];
    BOOL isNets=[IsNetWork isNetworkReachable];
    if(isNets==NO){//沒有網絡}
    else{//有網絡}

29 
開發中經常用到的sql語句:
INSERT OR REPLACE INTO 
PhoneInfo (ID,Logo,Name) VALUES (%d,'%@','%@') //添加
Update MobPhoneInfo ID=%d,Logo='%@',Name='%@' //修改
Delete from 
PhoneInfo //刪除表中所有數據
Delete from PhoneInfo  Where Id=2 //刪除表中Id=2數據

SELECT * FROM PhoneInfo Order By ID desc //按ID從大到小的順序查詢所有數據
SELECT ID FROM PhoneInfo Order By ID desc limit 0,1 //查詢最大ID
SELECT * FROM PhoneInfo where Name like '三星//查詢name爲三星的所有數據
SELECT * FROM PhoneInfo where NetFlow like '%%上網%%' //查詢netflow中包含“上網”的所有數據
SELECT count(Name) num FROM 
PhoneInfo where Id=2 //查詢id=2的數據總
SELECT * FROM PhoneInfo ORDER BY RANDOM() limit 3 //查詢3條隨機的數據

30 字符串相關:
30.1追加字符
NSMutableString *string = [[NSMutableString alloc] init];

string=@“你好”;
[string appendFormat:@"中國"];

30.2字符串替換:把info中所有的<都替換成#
NSString *stroneIntro=[info stringByReplacingOccurrencesOfString:@"<" withString:@"#"];

30.3字符串比較:

Bool IsTrue=[
@"Nob"isEqualToString:@"Mob"]

30.4
 不考慮大小寫比較字符串
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = =NSOrderedSame;

30.5 
改變字符串的大小寫
NSString *string1 = @"A String"; 

NSString *string2 = @"String"; 
NSLog(@"string1:%@",[string1uppercaseString]);//大寫
NSLog(@"string2:%@",[string2lowercaseString]);//小寫
NSLog(@"string2:%@",[string2capitalizedString]);//首字母大小

30.6
在串中搜索子串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSStringstringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];

30.7 
抽取子串
//-substringToIndex: 從字符串的開頭一直截取到指定的位置,但不包括該位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);

//-substringFromIndex:以指定位置開始(包括指定位置的字符),幷包括之後的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1substringFromIndex:3];
NSLog(@"string2:%@",string2);

//-substringWithRange: //按照所給出的位置,長度,任意地從字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0,4)];
NSLog(@"string2:%@",string2);


30.8在已有字符串中按照所給出範圍和長度刪除字符
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0,5)];
NSLog(@"String1:%@",String1);

30.9
在已有字符串後面在所指定的位置中插入給出的字符串 
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);

30.10 
將已有的空符串換成其它的字符串
//-setString:
NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);

30.11
按照所給出的範圍,和字符串替換的原有的字符
//-setString:
NSMutableString *String1 = [[NSMutableString alloc]initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4)withString:@"That"];
NSLog(@"String1:%@",String1);

30.12 
-判斷字符串內是否還包含別的字符串(前綴,後綴)
//01:檢查字符串是否以另一個字符串開頭- (BOOL) hasPrefix: (NSString *)aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES"): NSLog(@"NO");
//02:查找字符串某處是否包含其它字符串 - (NSRange) rangeOfString: (NSString *)aString,這一點前面在串中搜索子串用到過;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章