ios学习流水账2

1、UISearchBar自定义背景、取消按钮中文设置

C代码  收藏代码
  1. UISearchBar *seachBar=[[UISearchBar alloc] init];  
  2.   
  3. //修改搜索框背景  
  4. seachBar.backgroundColor=[UIColor clearColor];  
  5.   
  6. //去掉搜索框背景  
  7. [[searchbar.subviews objectAtIndex:0]removeFromSuperview];  
  8.   
  9. for (UIView *subview in seachBar.subviews)   
  10. {    
  11. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  
  12. {    
  13. [subview removeFromSuperview];    
  14. break;    
  15. }    
  16. }   
  17.   
  18. //自定义背景  
  19. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]];  
  20. [searchBar insertSubview:imageView atIndex:1];  
  21.   
  22. //输入搜索文字时隐藏搜索按钮,清空时显示  
  23. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {    
  24. searchBar.showsScopeBar = YES;    
  25. [searchBar sizeToFit];    
  26. [searchBar setShowsCancelButton:YES animated:YES];    
  27. return YES;    
  28. }    
  29.   
  30. - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {    
  31. searchBar.showsScopeBar = NO;    
  32. [searchBar sizeToFit];    
  33. [searchBar setShowsCancelButton:NO animated:YES];    
  34. return YES;    
  35. }    
  36.   
  37. //修改UISearchBar取消按钮中文字体  
  38.     for (id aa in [searchBar subviews]) {  
  39.         if ([aa isKindOfClass:[UIButton class]]) {  
  40.             UIButton *btn = (UIButton *)aa;  
  41.             [btn setTitle:@"取消" forState:UIControlStateNormal];  
  42.         }  
  43.     }  

 

2、UISearchBar

C代码  收藏代码
  1. #pragma mark 搜索控件  
  2. //搜索  
  3. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{  
  4.     UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];  
  5.     FirstViewController *fVC = [mainStory instantiateViewControllerWithIdentifier:@"goFirstView"];  
  6.     fVC.showStr = self.searchBar.text;  
  7.     [self presentModalViewController:fVC animated:YES];  
  8. }  
  9.   
  10. //搜索输入内容时触发  
  11. - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{  
  12.       
  13. }  
  14.   
  15. //焦点进入搜索  
  16. - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{  
  17.     self.soundBtn.hidden = YES;  
  18.     [self searchBar:searchBar activate:YES];  
  19.       
  20. }  
  21.   
  22. //取消搜索按钮  
  23. - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {  
  24.     self.searchBar.text = @"";  
  25.     self.soundBtn.hidden = NO;  
  26.     [self searchBar:searchBar activate:NO];  
  27. }  
  28.   
  29. - (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active{  
  30.     if (!active) {    
  31.         [self.searchBar resignFirstResponder];    
  32.     }   
  33.       
  34.     [self.searchBar setShowsCancelButton:active animated:YES];  
  35.     //修改UISearchBar取消按钮字体  
  36.     for (id aa in [searchBar subviews]) {  
  37.         if ([aa isKindOfClass:[UIButton class]]) {  
  38.             UIButton *btn = (UIButton *)aa;  
  39.             [btn setTitle:@"取消" forState:UIControlStateNormal];  
  40.         }  
  41.     }  
  42. }  

 

3、NSNotificationCenter通告中心

   NSNotificationCenter通告与一个按钮中用addTarget绑定方法有些相似,按钮中的绑定触发事件时才调用关联消息。而NSNotificationCenter的范围则大得多,比如说摇动中,当触发摇动事件时,就提交并触发消息。

示例如下:

C代码  收藏代码
  1. //1、需触发的消息方法  
  2. - (void) testNotification{  
  3.     NSLog(@"测试通告中心。。。。。。。");  
  4. }  
  5.   
  6. - (void)viewDidLoad  
  7. {  
  8.     [super viewDidLoad];  
  9.     //2、对象注册,关联消息  
  10.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testNotification) name:@"Dwen_Notification" object:nil];  
  11.       
  12. }  
  13.   
  14. #pragma 通告中心  
  15. - (IBAction)notificationTest:(id)sender {  
  16.     //3、提交消息  
  17.     [[NSNotificationCenter defaultCenter] postNotificationName:@"Dwen_Notification" object:@"Request Object"];  
  18. }  

 

4、视图显示和消失将会调用的方法,可处理一些前一界面的还原初始状态。

- (void) viewWillDisappear:  视图消失时调用

- (void) viewWillAppear:      进入视图时调用

例如:点击搜索后,会的取消按钮,跳到下一界面再返回时,需把取消按钮隐藏掉可通过它们处理。

 

5、获取程序的AppDelegate

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

 

6、ios4跳转界面

C代码  收藏代码
  1. QuotaManageViewController *qmvc = [[QuotaManageViewController alloc] init];  
  2.    [self.navigationController pushViewController:qmvc animated:YES];  
  3.    [qmvc release];  

 

7、UILable换行

C代码  收藏代码
  1. lab_1.lineBreakMode = UILineBreakModeWordWrap;  
  2. lab_1.numberOfLines = 0;  

 

8、ipad中设置tableview背景无效,有两种解决方法:

方法一:

C代码  收藏代码
  1. UIView *view = [[UIView alloc] init];  
  2.     view.backgroundColor = [UIColor clearColor];  
  3.     tableView2.backgroundView = view;  

 方法二:

C代码  收藏代码
  1. if ([self.tableView2 respondsToSelector:@selector(backgroundView)]) {  
  2.         self.tableView2.backgroundView = nil;  
  3.     }  

 

9、自定义UIView时,会继承它,下面是如何加载UIView对应xib。

C代码  收藏代码
  1. UIView *head_View = [[[NSBundle mainBundle] loadNibNamed:@"HeadView" owner:self options:nil] lastObject];  
  2.     head_View.frame = CGRectMake(0, -120, self.view.frame.size.width, 150);  
  3.     head_View.backgroundColor = [UIColor brownColor];  
  4.     [self.view addSubview:head_View];  

 

10、对UIView进行旋转。2012-11-07

今天在做ipad时,弹出的pop视图,总是横屏放着,不能竖屏。纠结了些时间,后来通过修改视图的坐标解决了该问题。

C代码  收藏代码
  1. CGAffineTransform at = CGAffineTransformMakeRotation(M_PI/2);//顺时钟旋转90  
  2.         at = CGAffineTransformTranslate(at, 200, 0);  
  3.         [popVC.view setTransform:at];  

 

11、关于在UIView上进行滑动手势和点击事件手势。

 

C代码  收藏代码
  1. ///////////注flowView为UIView//////////  
  2.  //添加滑动手势事件  
  3.     UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:flowView action:@selector(handleGesture:)];  
  4.     [flowView addGestureRecognizer:gestureRecognizer];  
  5.       
  6.     //添加点击手势事件  
  7.     flowView.userInteractionEnabled = YES;  
  8.     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:flowView action:@selector(goBigImageView)];  
  9.     [flowView addGestureRecognizer:tap];  

 

C代码  收藏代码
  1. //滑动手势(里面有手势的不同状态,根据需要进行灵活运用)  
  2. - (void)handleGesture:(UIPanGestureRecognizer *)recognizer {  
  3.     //UITapGestureRecognizer  
  4.    if (recognizer.state == UIGestureRecognizerStateChanged){  
  5.        NSLog(@"UIGestureRecognizerStateChanged");  
  6.    }else if(recognizer.state == UIGestureRecognizerStateEnded){  
  7.        NSLog(@"UIGestureRecognizerStateEnded");  
  8.    }else if(recognizer.state == UIGestureRecognizerStateBegan){  
  9.        NSLog(@"UIGestureRecognizerStateBegan");  
  10.    }else if(recognizer.state == UIGestureRecognizerStateCancelled){  
  11.        NSLog(@"UIGestureRecognizerStateCancelled");  
  12.    }else if(recognizer.state == UIGestureRecognizerStateFailed){  
  13.        NSLog(@"UIGestureRecognizerStateFailed");  
  14.    }else if(recognizer.state == UIGestureRecognizerStatePossible){  
  15.        NSLog(@"UIGestureRecognizerStatePossible");  
  16.    }else if(recognizer.state == UIGestureRecognizerStateRecognized){  
  17.        NSLog(@"UIGestureRecognizerStateRecognized");  
  18.    }  
  19.   
  20. }  

 

12、启动时异常

Couldn't register com.yourcompany.ReciteWords with the bootstrap server. Error: unknown error code.
This generally means that another instance of this process was already running or is hung in the debugger

解决方法,重启设备或模拟器

 

13、读写plist文件

C代码  收藏代码
  1. - (void)readWritePlist{  
  2.          //获取路径  
  3.          NSString *homePath = [[NSBundle mainBundle] executablePath];  
  4.     NSArray *strings = [homePath componentsSeparatedByString: @"/"];  
  5.     NSString *executableName  = [strings objectAtIndex:[strings count]-1];    
  6.     NSString *baseDirectory = [homePath substringToIndex:  
  7.                                [homePath length]-[executableName length]-1];      
  8.     //data.plist文件  
  9.     NSString *filePath = [NSString stringWithFormat:@"%@/data.plist",baseDirectory];  
  10.     NSLog(@"filePath: %@",filePath);  
  11.     NSMutableDictionary *dataDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];  
  12.     NSLog(@"dataDict: %@",dataDict);  
  13.     //change the value or add the value  
  14.     [dataDict setObject:@"YES" forKey:@"Trial"];  
  15.         [dataDict setValue:@"dwen" forKey:@"nickName"];  
  16.         //write back to data.plist file  
  17.     [dataDict writeToFile:filePath atomically:NO];  
  18.   
  19. }  

 

14、ios生成随机数(三种方式):

 int i = rand() % 5;

int i = random() % 5;

int  x = arc4random() % 100;//[0,100] 包括0 ,不包括100

int  y = (arc4random() % 501) + 500;//[500,1000 ],包括500 ,不包括1000

 

15、UIImage图片处理:缩放、设定大小、存储 (转载)

C代码  收藏代码
  1. //1.等比率缩放  
  2. - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{  
  3. UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);  
  4. [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];  
  5. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
  6. UIGraphicsEndImageContext();  
  7. return scaledImage;  
  8. }  
  9.   
  10. //2.自定长宽  
  11. - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize{  
  12. UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));  
  13. [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];  
  14. UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();  
  15. UIGraphicsEndImageContext();  
  16. return reSizeImage;  
  17. }  
  18.   
  19. //3.处理某个特定View  
  20. 只要是继承UIView的object 都可以处理  
  21. 必须先import QuzrtzCore.framework  
  22. -(UIImage*)captureView:(UIView *)theView{  
  23. CGRect rect = theView.frame;  
  24. UIGraphicsBeginImageContext(rect.size);  
  25. CGContextRef context = UIGraphicsGetCurrentContext();  
  26. [theView.layer renderInContext:context];  
  27. UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
  28. UIGraphicsEndImageContext();  
  29. return img;  
  30. }  
  31.   
  32. //4.储存图片  
  33. 储存图片这里分成储存到app的文件里, 储存到手机的图片库里  
  34. // 储存到app的文件里  
  35. NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];  
  36. [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];  

 

16、object-c正则验证,方式之一,如下:

    NSPredicate用于指定过滤器的条件。通过该对象准确地描述所需条件,对每个对象通过谓词进行筛选,判断它们是否与条件相匹配。

C代码  收藏代码
  1. NSString *numberRegex = @"[1-4]";  
  2. NSPredicate *number = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numberRegex];       
  3. bool result = [number evaluateWithObject:@"1"];  

 

 

17、定义程序块

C代码  收藏代码
  1. //定义程序块  
  2.     void(^loggerBlock)(void);  
  3.     //实现程序块  
  4.     loggerBlock = ^{NSLog(@"i am test block.");};  
  5.     //执行程序块  
  6.     loggerBlock();  

 

18、反射

C代码  收藏代码
  1. //反射(对CodingVo类进行反射)  
  2.     Class cls = NSClassFromString(@"CodingVo");  
  3.     id obj = [[cls alloc] init];  
  4.     SEL selector = NSSelectorFromString(@"test1");  
  5.    //调用CodingVo类中test1方法  
  6.     [obj performSelector:selector withObject:nil];  

 

19、调用短信、电话、邮件、Safari浏览器API

C代码  收藏代码
  1. //调用短信  
  2. - (IBAction)callMsg:(id)sender {  
  3.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://135587"]];  
  4. }  
  5.   
  6. //调用电话  
  7. - (IBAction)callTel:(id)sender {  
  8.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://135587"]];  
  9. }  
  10.   
  11. //调用Safari浏览器  
  12. - (IBAction)callSafari:(id)sender {  
  13.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com/"]];  
  14. }  
  15.   
  16. // 调用email  
  17. - (IBAction)callEmail:(id)sender {  
  18.     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://test@126.com"]];  
  19. }  

 

20、操作声音文件,添加AudioToolbox.framework库。

#import <AudioToolbox/AudioToolbox.h>

@property (assign,nonatomic) SystemSoundID soundID;

//测试播放声音
- (void) testSound{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"caf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
    AudioServicesPlaySystemSound(soundID);
    NSLog(@"testSound...mp3音频文件格式不支持");
}

 

21、ios触摸事件监听和操作(UITouch)

C代码  收藏代码
  1. //手指触摸屏幕时报告  
  2. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  
  3.     NSLog(@"touchesBegan");  
  4. }  
  5. //手指在屏幕上移动时报告  
  6. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{  
  7.      NSLog(@"touchesMoved");  
  8. }  
  9. //手指离开屏幕时报告  
  10. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{  
  11.      NSLog(@"touchesEnded");  
  12. }  
  13. //因接听电话或其他因素导致取消触摸时报告  
  14. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{  
  15.      NSLog(@"touchesCancelled");  
  16. }  

 

22、\u00B0表示角度符号

NSString *latitudeStr = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.latitude];

 

 23、实现ios动画效果有两种方式,一种是UIView层面上的,另一种是CATransation更底层次控制

 

24、设备信息

NSLog(@"系统名称:%@ ,系统版本: %@",[[UIDevice currentDevice] systemName],[[UIDevice currentDevice] systemVersion]);

 

 25、MPMoviePlayerViewController控件中,需把Done改为中文,对工程中plist文件进行设置。对其他英文控件中的英文文本也有同样有效。设置见下图:



 

 26、文本层CATextLayer

 

C代码  收藏代码
  1. CATextLayer *txtLayer = [CATextLayer layer];  
  2.         txtLayer.string = @"地球";  
  3.         txtLayer.foregroundColor = [[UIColor redColor] CGColor];  
  4.         txtLayer.bounds = CGRectMake(0, 0, 200, 50);  
  5.         txtLayer.position = CGPointMake(0, 0);  
  6.         [imageLayer addSublayer:txtLayer];  
 

 27、assign、retain、copy、readonly区别

assign:指定setter方法用简单的赋值,这是默认操作。你可以对标量类型(如int)使用这个属性。你可以想象一个float,它不是一个对象,所以它不能retain、copy。

retain:指定retain应该在后面的对象上调用,前一个值发送一条release消息。你可以想象一个NSString实例,它是一个对象,而且你可能想要retain它。

copy:指定应该使用对象的副本(深度复制),前一个值发送一条release消息。基本上像retain,但是没有增加引用计数,是分配一块新的内存来放置它。

readonly:将只生成getter方法而不生成setter方法(getter方法没有get前缀)。

readwrite:默认属性,将生成不带额外参数的getter和setter方法(setter方法只有一个参数)。

atomic:对于对象的默认属性,就是setter/getter生成的方法是一个原子操作。如果有多个线程同时调用setter的话,不会出现某一个线程执行setter全部语句之前,另一个线程开始执行setter的情况,相关于方法头尾加了锁一样。

nonatomic:不保证setter/getter的原子性,多线程情况下数据可能会有问题
28、声明关键字前加extern,为外部变量。外部变量可被其他任何方法或函数访问和更改其值的变量。

 

00

posted @ 2013-07-11 10:12  子福当自强  阅读(424)  评论(0编辑  收藏  举报
悟道