`
wenxin2009
  • 浏览: 314431 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ios学习流水账1

    博客分类:
  • ios
阅读更多

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

  • 大小: 16.2 KB
  • 大小: 244.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics