iPhone开发技巧小结(一)

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://*"]];

14 双引号转义:
用 \" 表示 双引号

15 设置按钮按下换图片 松开还是原图
//                [danxuan setImage:[UIImage imageNamed:@"exercise_option_n.png"] forState:UIControlStateNormal];
                //这个是设置按下的图片,松开就是上面的图片
//               [danxuan setImage:[UIImage imageNamed:@"exercise_option_s.png"] forState:UIControlEventTouchDragOutside];

16  判断数组中是否存在某元素:
BOOL isValue=[keyArray containsObject:@"aaa"];

17 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)//删除:
 {
}
}

18 数组相关:
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];//禁止触发点击某行

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];

转载自:http://www.cocoachina.com/bbs/read.php?tid=125836


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值