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

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

2、UISearchBar

  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. - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  11. }
  12. //焦点进入搜索
  13. - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
  14. self.soundBtn.hidden = YES;
  15. [self searchBar:searchBar activate:YES];
  16. }
  17. //取消搜索按钮
  18. - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  19. self.searchBar.text = @"";
  20. self.soundBtn.hidden = NO;
  21. [self searchBar:searchBar activate:NO];
  22. }
  23. - (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active{
  24. if (!active) {
  25. [self.searchBar resignFirstResponder];
  26. }
  27. [self.searchBar setShowsCancelButton:active animated:YES];
  28. //修改UISearchBar取消按钮字体
  29. for (id aa in [searchBar subviews]) {
  30. if ([aa isKindOfClass:[UIButton class]]) {
  31. UIButton *btn = (UIButton *)aa;
  32. [btn setTitle:@"取消" forState:UIControlStateNormal];
  33. }
  34. }
  35. }

3、NSNotificationCenter通告中心

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

示例如下:

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

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

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

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

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

5、获取程序的AppDelegate

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

6、ios4跳转界面

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

7、UILable换行

  1. lab_1.lineBreakMode = UILineBreakModeWordWrap;
  2. lab_1.numberOfLines = 0;

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

方法一:

  1. UIView *view = [[UIView alloc] init];
  2. view.backgroundColor = [UIColor clearColor];
  3. tableView2.backgroundView = view;

方法二:

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

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

  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视图,总是横屏放着,不能竖屏。纠结了些时间,后来通过修改视图的坐标解决了该问题。

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

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

  1. ///////////注flowView为UIView//////////
  2. //添加滑动手势事件
  3. UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:flowView action:@selector(handleGesture:)];
  4. [flowView addGestureRecognizer:gestureRecognizer];
  5. //添加点击手势事件
  6. flowView.userInteractionEnabled = YES;
  7. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:flowView action:@selector(goBigImageView)];
  8. [flowView addGestureRecognizer:tap];
  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. }

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文件

  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. }

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图片处理:缩放、设定大小、存储 (转载)

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

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

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

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

17、定义程序块

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

18、反射

  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

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

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)

  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

  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

ios学习流水账2的更多相关文章

  1. ios学习流水账1

    1.UIImageview设边框.圆角 需要引QuartzCore/QuartzCore.h> //设UIImageView边框 CALayer *layer = [m_imgView laye ...

  2. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  3. 【原】iOS学习之事件处理的原理

    在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...

  4. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  5. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

  6. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  7. 黑苹果-IOS学习的开始

    深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...

  8. iOS 学习资源

    这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...

  9. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

随机推荐

  1. 【转】PHP对象在内存中的分配

    对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上 说大体上是分为4 段,栈空间段.堆空 ...

  2. ES6 学习体会

    第一部分: 1.初始化项目 npm init -y 2.安装ES6 环境 .babelrc 文件 babel-cli -g babel-ecmascript2015 babel-cli --save- ...

  3. altium designer同一工程多个原理图如何快速查找同一网络标号

    方法一:如果只知道网络标号的名称,尚未找到任何一个,可以:Ctrl+F,输入网络标号名称,可按顺序逐个查看各个网络标号. 方法二:如果已经看到一个所要查找的网络标号,可以:按住Alt键不放,鼠标左键单 ...

  4. [bzoj3270] 博物馆 [期望+高斯消元]

    题面 传送门 思路 本题的点数很少,只有20个 考虑用二元组$S=(u,v)$表示甲在$u$点,乙在$v$点的状态 那么可以用$f(S)$表示状态$S$出现的概率 不同的$f$之间的转移就是通过边 转 ...

  5. Semaphore built from mutex in C++11

    #include <mutex> #include <condition_variable> using namespace std; class semaphore{ pri ...

  6. js Event对象

    (事件阶段)Event Phases Event对象在event第一次触发的时候被创建出来,并且一直伴随着事件在DOM结构中流转的整个生命周期.event对象会被作为第一个参数传递给事件监听的回调函数 ...

  7. Hibernate中多种方式解除延迟加载

    问题引发:因为dao使用load(),默认延迟加载的,当在biz关闭session之后,UI层无法获取对象的非id属性值 解决方案: 1.变成get,即时加载 2.用Hibernate.isIniti ...

  8. 简述web工程师的职责与学习

    最近两年web突然很火,也有很多人涌入这一行,但这行实际上是进来的人很多,出去的人也很多. 在我眼里,Web前端开发工程师的职责有:1.Web前端表现层及与前后端交互的架构设计和开发2.配合后台开发人 ...

  9. flask框架基本使用(3)(session与cookies)

    #转载请留言联系 flask 框架基本使用(1):https://www.cnblogs.com/chichung/p/9756935.html flask 框架基本使用(2):https://www ...

  10. java的unity单元测试

    import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestUnit { publ ...