ios学习流水账1
1、UIImageview设边框、圆角
需要引QuartzCore/QuartzCore.h>
- //设UIImageView边框
- CALayer *layer = [m_imgView layer];
- [layer setMasksToBounds:YES];
- layer.cornerRadius = 10.0;//设圆角
- [layer setBorderWidth:1];
- [layer setBorderColor:[[UIColor blackColor] CGColor]];
2、bounds属性和frame属性区别
frame指的是:该view在父view坐标系统中的位置和大小.
bounds指的是:该view在本身坐标系统中的位置和大小.
3、导航navigationController设置按钮背景图片
- UIImage *backImage = [UIImage imageNamed:@"ip_bt-back.png"];
- UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, backImage.size.width, backImage.size.height)];
- [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
- [backBtn setBackgroundImage:backImage forState:UIControlStateNormal];
- UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
- self.navigationItem.leftBarButtonItem = leftBtn;
4、ios5加载自定义单元格
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"priceRecordCell";
- //加载nib
- static BOOL nibsRegistered = NO;
- if (!nibsRegistered) {
- UINib *nib = [UINib nibWithNibName:@"PriceRecordCell" bundle:nil];
- [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
- nibsRegistered = YES;
- }
- PriceRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[PriceRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- // Configure the cell...
- cell.nickName.text = @"爱我中华";
- cell.priceLab.text = @"价格 ¥100.00";
- cell.lastTimeLab.text = @"2012-10-15 18:00:08";
- return cell;
- }
5、tableview单元格分割线设置
//不需要分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
6、ios4工程在ios5上运行会出现ARC问题,在Compile Sources 下的Compiler Flags属性中添加-fno-objc-arc,可解决该问题 。
7、UIImageView 上添加点击事件
- UIImageView *imageView =[[UIImageView alloc ]initWithFrame :CGRectMake (100 , 100 , 200 , 200 )];
- imageView. image =[ UIImage imageNamed : @"test.png"];
- //事件可用设置
- imageView. userInteractionEnabled = YES ;
- UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (clickImage)];
- [imageView addGestureRecognizer :tap];
8、ios5 UIView 设圆角
需添加#import <QuartzCore/QuartzCore.h>
centerView.layer.cornerRadius = 6.0f;
9、获取文件路径
- //获取沙盒路径
- NSString *sandboxPath = NSHomeDirectory();
- NSLog(@"sandboxPath :%@",sandboxPath);
- NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];
- NSLog(@"documentPath : %@",documentPath);
- //获取沙盒中的文件目录
- NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- //遍历得到的路径
- for (int i=0; i<[documentDirectories count]; i++) {
- NSLog(@"%@",[documentDirectories objectAtIndex:i]);
- }
10、自定义单元格中,如果自定义单元格中有按钮点击事件,不可设置以下属性,会阻塞按钮事件解发。 (2012-10-16)
cell_1.userInteractionEnabled = NO;//单元格不可点击
11、内存过低警告,清除内存
通过消息通知方式提示并清除
- - (id)init{
- //当内存过低时,清空内存
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
- [nc addObserver:self selector:@selector(clearCache) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
- }
12、NSBundle可以获取当前程序的资源,得到它,可以获取当前资源中的相关文件,并进行操作。
- //NSBundle获取当前应用资源
- NSBundle *appBundle = [NSBundle mainBundle];
- //bundle得到文件
- NSString *path = [appBundle pathForResource:@"first" ofType:@"png"];
- if (path != nil) {
- NSLog(@"path : %@",path);
- }
- //bundle得到类
- Class class = [appBundle classNamed:@"Test"];
- Test *tinstance = [[class alloc] init];
- [tinstance test];
- //bundle加载xib
- [appBundle loadNibNamed:@"ThirdViewController" owner:self options:nil];
- //bundle中还有很多其它方法,可查api
13、应用状态、状态切换,通过它可了解应用在运行时的各个状态。
常见应用中多种状态:末运行--激活--末激活--后台运行--暂停--激活
- //启动应用时调用
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //...... NSLog(@"====didFinishLaunchingWithOptions.........%@",NSStringFromSelector(_cmd));
- return YES;
- }
- //按下主窗口按钮时调用(末激活)
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- NSLog(@"====applicationWillResignActive");
- }
- //按下主窗口按钮时调用(后台运行,末激活,5秒后进入暂停)
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- //进行入后运行时,该方法是保存数据的最佳方法
- //[object saveChanges];
- NSLog(@"====applicationDidEnterBackground");
- }
- //按主窗口按钮退出后,再按下应用图标时调用(激活)
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- NSLog(@"====applicationWillEnterForeground");
- }
- //按主窗口按钮退出后,再按下应用图标时调用(激活)
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- NSLog(@"====applicationDidBecomeActive");
- }
- //ios4之前实现该方法,在该方法保存数据最佳
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- NSLog(@"====applicationWillTerminate");
- }
14、沙盒理解
每个ios应用都有自己的应用沙盒(application sandbox),应用沙盒就是文件系统目录,但与文件系统其他部分隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒。
15、navigation导航
- //设置Navigation Bar背景
- UIImage *title_bg = [UIImage imageNamed:@"ip_titelbar.png"];
- [self.navigationController.navigationBar setBackgroundImage:title_bg forBarMetrics:UIBarMetricsDefault];
- UIView *_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 360, 40)];
- UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 20)];
- titleLab.text = @"快速竞拍";
- titleLab.textColor = [UIColor redColor];
- titleLab.font = [UIFont fontWithName:@"Arial" size:18];
- titleLab.backgroundColor = [UIColor clearColor];
- [_view addSubview:titleLab];
- resultLab = [[UILabel alloc] initWithFrame:CGRectMake(122, 20, 100, 20)];
- resultLab.backgroundColor = [UIColor clearColor];
- resultLab.font = [UIFont fontWithName:@"Arial" size:14];
- [_view addSubview:resultLab];
- //刷新按钮
- UIButton *refreshBtn = [[UIButton alloc] initWithFrame:CGRectMake(255, 8, 50, 30)];
- [refreshBtn setBackgroundImage:[UIImage imageNamed:@"ip_bt_shuaxin.png"] forState:UIControlStateNormal];
- [refreshBtn addTarget:self action:@selector(refurbish) forControlEvents:UIControlEventTouchUpInside];
- [_view addSubview:refreshBtn];
- self.navigationItem.titleView = _view;
- //显示导航
- // self.navigationController.navigationBarHidden = NO;
16、ipad中UITabBarController添加ViewController。
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
- UINavigationController *masterNavigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
- DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
- UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
- masterViewController.detailViewController = detailViewController;
- self.splitViewController = [[UISplitViewController alloc] init];
- self.splitViewController.delegate = detailViewController;
- self.splitViewController.viewControllers = [NSArray arrayWithObjects:masterNavigationController, detailNavigationController, nil];
- //首页barTab
- UITabBarController *tabBarController = [[UITabBarController alloc] init];
- self.splitViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"f.jpg"] tag:0];
- //购物车barTab
- CartViewController *cartVC = [[CartViewController alloc] initWithNibName:@"CartViewController" bundle:nil];
- cartVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购物车" image:[UIImage imageNamed:@"f.jpg"] tag:0];
- //条码购barTab
- BarCodeViewController *barcodeVC = [[BarCodeViewController alloc] initWithNibName:@"BarCodeViewController" bundle:nil];
- barcodeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"条码购" image:[UIImage imageNamed:@"f.jpg"] tag:0];
- //更多barTab
- MoreViewController *moreVC = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];
- moreVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"f.jpg"] tag:0];
- //controller数组
- NSArray *controllers = [NSArray arrayWithObjects:self.splitViewController,cartVC,barcodeVC,moreVC, nil];
- tabBarController.viewControllers = controllers;
- self.window.rootViewController = tabBarController;
- // self.window.rootViewController = self.splitViewController;
- [self.window makeKeyAndVisible];
- return YES;
- }
17、如何获取应用存放文件根路径:
- //根路径
- NSString *homePath = [[NSBundle mainBundle] executablePath];
- NSArray *strings = [homePath componentsSeparatedByString: @"/"];
- NSString *executableName = [strings objectAtIndex:[strings count]-1];
- NSString *baseDirectory = [homePath substringToIndex:
- [homePath length]-[executableName length]-1];
- NSString *fileName = [NSString stringWithFormat:@"%@/test.txt",baseDirectory];
- NSLog(@"filePath: %@",fileName);
18、block类似匿名函数
以为例子:
- static int outA = 8;
- static NSString *c=@"";
- int(^myPtr)(int,NSString *)=^(int a,NSString *b){
- c=b;
- return outA+a;
- };
- outA = 5;//改变outA
- int result = myPtr(2,@"test");//结果为10,outA是复制的,后面改变outA无效
- NSLog(@"result : %i",result);
19、performSelector的应用,它可直接调用实例的方法,能延迟执行方法时间。
- - (void) test:(NSString *) str;
- - (void) test:(NSString *) str{
- NSLog(@"you input is : %@",str);
- }
- //调用方法
- [self performSelector:@selector(test:) withObject:@"dwen"];
- [self performSelector:@selector(test:) withObject:@"wen" afterDelay:2.0f];//两秒后执行
20、UIButton中addTarget中传参问题,该事件可以通过setTag方法进行传整数。
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
- //action参数中写入事件执行方法
- [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
- //在button的tag中添加你需要传递的参数
- [button setTag:100];
- //action方法
- -(void)action:(id)sender{
- int i = [sender tag];
- }
21、沙盒中包含三个文件夹,Documents 、Library和tmp
- //获取Documents目录
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
- NSString *documentDirectory = [paths objectAtIndex:0];
- NSLog(@"%@",documentDirectory);
- //获取当前应用中文件路径
- NSString *filename = [documentDirectory stringByAppendingPathComponent:@"w12122.jpg"];
- NSLog(@"%@",filename);
- //获取tmp目录
- NSString *tempPath = NSTemporaryDirectory();
- NSString *tempFile = [tempPath stringByAppendingPathComponent:@"w12122.jpg"];
- NSLog(@"tempFile :%@",tempFile);
22、NSString截取字符串
NSString *str = @"B12121.jpg";
NSRange range = [str rangeOfString:@"."];
NSLog(@"%i",range.location);
NSLog(@"%@",[str substringToIndex:range.location]);
23、UINavigationBar
- //创建导航栏集合
- UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
- //创建一个左边按钮
- UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回"
- style:UIBarButtonItemStyleBordered
- target:self
- action:@selector(back)];
- //设置导航栏内容
- [navigationItem setTitle:@"拍品列表"];
- //把导航栏集合添加入导航栏中,设置动画关闭
- [navigationBar pushNavigationItem:navigationItem animated:NO];
- //把左右两个按钮添加入导航栏集合中
- [navigationItem setLeftBarButtonItem:leftButton];
24、在navigationController中添加右边多个按钮。效果图:
代码如下:
- //添加导航右边按钮
- UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
- [tools setTintColor:[self.navigationController.navigationBar tintColor]];
- [tools setAlpha:[self.navigationController.navigationBar alpha]];
- NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
- UIBarButtonItem *firstBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSpecial)];
- [buttons addObject:firstBtn];
- [tools setItems:buttons animated:NO];
- UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools];
- self.navigationItem.rightBarButtonItem = myBtn;
25、循环删除uiview
- for (UIView *subview in scrollView.subviews) {
- if ([subview isKindOfClass:[CatalogView class]]) {
- [subview removeFromSuperview];
- }
- }
26、ios4以前内存管理
00
ios学习流水账1的更多相关文章
- ios学习流水账2
1.UISearchBar自定义背景.取消按钮中文设置 UISearchBar *seachBar=[[UISearchBar alloc] init]; //修改搜索框背景 seachBar.bac ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- 【原】iOS学习之事件处理的原理
在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
- iOS学习路线图
一.iOS学习路线图 二.iOS学习路线图--视频篇 阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天 学习后目标: ...
- 黑苹果-IOS学习的开始
深知安装黑苹果的不易,在这里写一下关于我的Thinkpad E430c安装黑苹果教程(Mac版本:Yosemite 10.10.4),希望能够帮助有需要的朋友. 首先贴上我的电脑配置报表: ----- ...
- iOS 学习资源
这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
随机推荐
- 2017 多校4 Security Check
2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...
- css字体投影
最近在整理学习CSS3的一些小知识,现在已经整理了CSS3选择器,CSS3圆角和CSS3元素阴影属性的使用方法了.今天为大家整理一下CSS3中的文字阴影——text-shadow的使用方法.希望能对大 ...
- 杭电oj2000-2011
2000 ASCII码排序 #include <stdio.h> int main(){ char a,b,c,t; while(scanf("%c%c%c", &a ...
- c /c++变参函数(转)
原文转自 https://blog.csdn.net/wwzcx/article/details/8940092 实现c/c++语言的变参函数. 变参函数 :void fun(para,...) 变参 ...
- 【友盟统计报表解读】之错误分析iOS版
http://bbs.umeng.com/thread-6908-1-1.html 错误分析功能说明1.概述 错误分析是友盟为移动开发者提供的Crash收集和分析工具,帮助开发者监测App在移动设备上 ...
- Kubernetes控制节点安装配置
#环境安装Centos 7 Linux release 7.3.1611网络: 互通配置主机名设置各个服务器的主机名hosts#查找kubernetes支持的docker版本Kubernetes v1 ...
- Android 利用Sharp样式设置文本框EditText圆角形状
1.首先新建样式文件editsharp.xml: <?xml version="1.0" encoding="utf-8"?> <shape ...
- PHP防止表单重复提交的解决方法
PHP+SESSION防止表单重复提交 index.php 当前表单页面is_submit设为0 SESSION_START(); $_SESSION['is_submit'] = 0; <fo ...
- json model 互转
1.json转model TestModel tm = new TestModel();JavaScriptSerializer js = new JavaScriptSerializer();tm ...
- (10)C#静态方法,静态字段,静态类,匿名类
6.静态方法 使用静态方法就可不必用类的实例化调用次函数 class Test { public static void method() { ........ } //当调用一个method()时就 ...