iOS开发知识点总结(连载中...)

2017-05-25  本文已影响0人  MrOreo
您好,世界!

1. 读取文件的大小


2. 读取手机的剩余内存空间

 #include <sys/param.h>
 #include <sys/mount.h>
 + (NSString *)freeDiskSpaceInBytes{
 struct statfs buf;
 unsigned long long freeSpace = -1;
 if (statfs("/var", &buf) >= 0) {
     freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
 }
     NSString *str = [NSString stringWithFormat:@"手机剩  余存储空间为:%0.2lld MB",freeSpace/1024/1024];
 return str;
 }

3. 构造方法(+alloc) 与 类方法的区别(MRC代码)

 id array = [NSMutableArray arrayWithCapacity:1];
 equals
 id array = [[[NSMutableArray alloc] initWithCapacity:1] autorelease];

4. __strong & __weak & __unsafe_unretained & __autoreleasing 的相互牵扯

读<OC高级编程>-内存管理章节总结


5. 属性声明的属性和所有权修饰符的对应关系

property_table.png

6. 设置导航条的相关属性


7. 给键盘添加上部的工具条,用于隐藏键盘

 // 充当弹簧条的效果
        UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneClk)];
        
        UIToolbar *bar = [[UIToolbar alloc] init];
#warning 1.一定设置尺寸,否则点击事件将不会生效;
        [bar sizeToFit];
        bar.tintColor = [UIColor blackColor];
        [bar setItems:@[item0, item]];
        self.inputAccessoryView = bar;

8. UITextField的text超过宽度,中间显示...

  1. 关于文本内容超过指定控件宽度且只显示一行时,默认会在末尾显示..., 只是lable的特性(lineBreakMode).
  2. 但是UITextField并没有此属性,因此可以自定义UITextField , 内部添加一个lable进行相应的控制;

.h文件

  
#import <UIKit/UIKit.h>

typedef void(^TextFieldDoneClkBlock)(void);

@interface NATextField : UITextField

@property (nonatomic, strong) UILabel *contentLbl;

@property (nonatomic, copy) TextFieldDoneClkBlock doneBlock;

@end

.m文件

#import "NATextField.h"

@implementation NATextField

- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) {
      
      UILabel *lineView = [[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height - 1, frame.size.width, 1)];
      
      lineView.backgroundColor = NARGB(227, 227, 227);
      
      _contentLbl = [[UILabel alloc] init];
      _contentLbl.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 1);
      _contentLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
      _contentLbl.hidden = YES;
      
      [self addSubview:_contentLbl];
      [self addSubview:lineView];
     
  }
  return self;
}

- (void)doneClk
{
  [self resignFirstResponder];
  
  if (self.doneBlock) {
      self.doneBlock();
  }
}

- (void)setLeftView:(UIView *)leftView
{
  [super setLeftView:leftView];
  
  _contentLbl.x = leftView.right;
  _contentLbl.w = self.w - leftView.right;
}

- (void)setFont:(UIFont *)font
{
  [super setFont:font];
  _contentLbl.font = font;
}

- (void)setText:(NSString *)text
{
  [super setText:text];
  _contentLbl.text = text;
}

- (void)layoutSubviews {
  [super layoutSubviews];
  [self bringSubviewToFront:_contentLbl];
}

@end

use

   _addressTF2.textColor = [UIColor clearColor];
   _addressTF2.contentLbl.hidden = NO;

9. Block使用copy的情况

block copy.png

10. NSTimer不执行的改进

dispatch_async(dispatch_get_global_queue(0, 0), ^{
      
      if (!_timer) {
          
          _timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(getNum) userInfo:nil repeats:YES];
          [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
          [[NSRunLoop currentRunLoop] run];
      }
  });
上一篇 下一篇

猜你喜欢

热点阅读