1、UIImageview设边框、圆角
需要引QuartzCore/QuartzCore.h>
-
- 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";
-
- 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];
- }
-
- 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 *appBundle = [NSBundle mainBundle];
-
- NSString *path = [appBundle pathForResource:@"first" ofType:@"png"];
- if (path != nil) {
- NSLog(@"path : %@",path);
- }
-
- Class class = [appBundle classNamed:@"Test"];
- Test *tinstance = [[class alloc] init];
- [tinstance test];
-
- [appBundle loadNibNamed:@"ThirdViewController" owner:self options:nil];
-
-
13、应用状态、状态切换,通过它可了解应用在运行时的各个状态。
常见应用中多种状态:末运行--激活--末激活--后台运行--暂停--激活
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
-
- return YES;
- }
-
-
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- NSLog(@"====applicationWillResignActive");
- }
-
-
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
-
-
- NSLog(@"====applicationDidEnterBackground");
- }
-
-
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- NSLog(@"====applicationWillEnterForeground");
- }
-
-
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- NSLog(@"====applicationDidBecomeActive");
- }
-
-
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- NSLog(@"====applicationWillTerminate");
- }
14、沙盒理解
每个ios应用都有自己的应用沙盒(application sandbox),应用沙盒就是文件系统目录,但与文件系统其他部分隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒。
15、navigation导航
-
- 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;
-
-
-
16、ipad中UITabBarController添加ViewController。
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
-
-
- 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];
-
-
- UITabBarController *tabBarController = [[UITabBarController alloc] init];
- self.splitViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"f.jpg"] tag:0];
-
- CartViewController *cartVC = [[CartViewController alloc] initWithNibName:@"CartViewController" bundle:nil];
- cartVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购物车" image:[UIImage imageNamed:@"f.jpg"] tag:0];
-
- BarCodeViewController *barcodeVC = [[BarCodeViewController alloc] initWithNibName:@"BarCodeViewController" bundle:nil];
- barcodeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"条码购" image:[UIImage imageNamed:@"f.jpg"] tag:0];
-
- MoreViewController *moreVC = [[MoreViewController alloc] initWithNibName:@"MoreViewController" bundle:nil];
- moreVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"f.jpg"] tag:0];
-
-
- NSArray *controllers = [NSArray arrayWithObjects:self.splitViewController,cartVC,barcodeVC,moreVC, nil];
- tabBarController.viewControllers = controllers;
- self.window.rootViewController = tabBarController;
-
-
- [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;
- int result = myPtr(2,@"test");
- 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)];
-
- [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
-
- [button setTag:100];
-
-
- -(void)action:(id)sender{
- int i = [sender tag];
- }
21、沙盒中包含三个文件夹,Documents 、Library和tmp
-
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
- NSString *documentDirectory = [paths objectAtIndex:0];
- NSLog(@"%@",documentDirectory);
-
- NSString *filename = [documentDirectory stringByAppendingPathComponent:@"w12122.jpg"];
- NSLog(@"%@",filename);
-
-
- 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];
- }
- }