IOS 入门
发布日期:2021-09-28 18:46:05 浏览次数:10 分类:技术文章

本文共 5910 字,大约阅读时间需要 19 分钟。

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

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8265231 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【一】从HelloWorld开始
下一篇:Java Web HelloWorld!

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月08日 04时46分53秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章