IOS 入门

http://mobile.51cto.com/mobile/objc/


Object-C中 NSInteger不是对象

Button动态点击事件:

[msgBtn addTarget:self action:@selector(sel_back:) forControlEvents:UIControlEventTouchUpInside];


页面跳转:

1.present:

vc_info = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
vc_info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:vc_info animated:YES completion:^{// 跳转
                NSLog(@"show InfoView!");
            }];
[self dismissViewControllerAnimated:YES completion:nil];// 回退  


带参数的页面跳转:

HelpPlayDetailViewController *helpPlayDetailVC = [[HelpPlayDetailViewController alloc] init];
[helpPlayDetailVC setPageNumber:0];
[self.navigationController pushViewController:helpPlayDetailVC animated:YES];
[helpPlayDetailVC release];


TableView点击一行跳转:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MessageDetailViewController *messageDetailVC = [[MessageDetailViewController alloc] init];
    [self.navigationController pushViewController:messageDetailVC animated:YES];
    [messageDetailVC release];
}


IOS 图片平移动画:

-(void)peachAnimation{
    UIImageView* peach = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"flower"]];
    [self.view addSubview:peach];
    //设置动画效果  
    CGContextRef context = UIGraphicsGetCurrentContext();
    //开始播放动画  
    [UIView beginAnimations:nil context:context]; 
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];  
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];  
    [UIView setAnimationDuration:1.5]; 
    [peach setFrame:CGRectMake(250, 400, peach.frame.size.width, peach.frame.size.height)];
    [UIView commitAnimations];
}


IOS定时器:

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(xxx) userInfo:nil repeats:YES];


AlertDialog

-(IBAction)showDialog{  
    NSLog(@"show dialog");  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误"  
		                                                            message:@"请检查您的网络链接!"  
		                                                             delegate:self  
		                                               cancelButtonTitle:@"确定"  
		                                               otherButtonTitles:nil];  
    [alert show];  
    [alert release];  
}  

UITextField提示字符:

myTextField.placeholder = @"请在此输入账号";


UITextField圆角:

有时候, 当点击输入框时, 你会发现光标就快挨着左边框了, 很不明显, 这是个很小的用户体验, 解决也很容易, 设置一下UITextField的边框样式

textField.borderStyle = UITextBorderStyleRoundedRect

这样就明显好看多了, 设置成这个样式后, 四周也会自带圆角, 并且有明显的凹凸效果


UITextField垂直居中:

cloundNameField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;


UITextField字体大小:

LY_Text.font = [UIFont fontWithName:@"helvetica" size:18];  //字体和大小设置


UITextField限制字符长度:

UITextField*nameTxetField=[[UITextField alloc] initWithFrame:CGRectMake(50,100, 200, 30)];

[nameTxetField addTarget:self action:@selector(limitTextlength:)forControlEvents:UIControlEventEditingCha nged];
-(void)limitTextlength:(UITextField *)textField{
      if([textField.text length]>16) {
              textField.text=[textField.text substringToIndex:16];
      }
}


字符串切割

componentsSeparatedByString


字符串替换

str_bangNum = [str_bangNum stringByReplacingOccurrencesOfString:@"A"withString:@"B"];


字符串修剪(过滤提取内容)stringByTrimmingCharactersInSet

NSString *textStr = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


字符串按策略分割componentsSeparatedByCharactersInSet

NSArray *arr = [searchText componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];


可拉伸的图片(同Android .9 nine-patch)

UIImage *btn_bg = [UIImageimageNamed:@"voice_tips_bg.png"];

btn_bg = [btn_bg stretchableImageWithLeftCapWidth:8topCapHeight:2];

[thrBtnsetBackgroundImage:btn_bgforState:UIControlStateNormal];

leftCapWidth 和 topCapHeight所指定的四边不会被拉伸
rightCapWIdth = image.width - leftCapWidth - 1, bottomCapHeight同理
如图中, 只有绿色部分会被拉伸缩放


view设置背景图片

self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"common_bg"]];


IOS消息通知(Java的Observer觀察者)

client:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"test" object:nil]; 

  1. - (void) test:(NSNotification*) notification  
  2. {  
  3. searchFriendArrary = [notification object];//通过这个获取到传递的对象  
  4. }  
Server:

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray]; 


NSString 转 NSMutableString

NSMutableString *str = [stringXXX  mutableCopy];


UITableView 去掉背景 分割线

[setting_table setBackgroundColor:[UIColor clearColor]];
[setting_table setSeparatorColor:[UIColor clearColor]];
 [setting_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];


UITableView ScrollTo

2
3
4
5
//设置NSIndexPath,其中section:0,row:4
NSIndexPath * ndxPath= [NSIndexPath indexPathForRow: 4 inSection: 0 ];
//将UITableView滚动动ndxPath指定的位置。
[contentTableView scrollToRowAtIndexPath:ndxPath
atScrollPosition:UITableViewScrollPositionTop  animated:YES];

2
3
4
5
//设置NSIndexPath,其中section:0,row:4
NSIndexPath * ndxPath= [NSIndexPath indexPathForRow: 4 inSection: 0 ];
//将UITableView滚动动ndxPath指定的位置。
[contentTableView scrollToRowAtIndexPath:ndxPath
atScrollPosition:UITableViewScrollPositionTop  animated:YES];

刷新当行数据

       NSIndexPath * uncheckIndexPath = [NSIndexPathindexPathForRow:indexinSection:section];

        [table_AllEvents_reloadRowsAtIndexPaths:@[uncheckIndexPath]

                               withRowAnimation:NO];


保存图片至GAllery

UIImageWriteToSavedPhotosAlbum([m_imageViewimage], nil,nil, nil);

           UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"" 

                                                           message:@"保存图片成功" 

                                                          delegate:nil 

                                                 cancelButtonTitle:@"确认" 

                                                 otherButtonTitles:nil];

            [alertshow];

            [alertrelease];



时间计算:

如计算当前日的前12小时时间:

NSDate *beg = [NSDate date];

NSDateComponents *comps = [[NSDateComponents alloc] init];

[comps setHour:-12];

NSDate *end = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] dateByAddingComponents:comps toDate:beg options:0];


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值