• IOS工作笔记(二)


    说明:记录下学习IOS的一些琐碎,有些在现在看起来很简单幼稚,不过权当学习足迹吧!

    1.懒加载(即延迟加载)只有被调用时才初始化,防止资源浪费,需要重写对象 的get方法,且必须写成成员变量形式,如_imageData。可以这么写,如:

     1 @property(nonatomic,strong) NSArray *imageData;
     2 
     3 -(NSArray *)imageData{ //重写imageData的get方法
     4     if(_imageData == nil){
     5         //初始化数据
     6         NSMutableDictionary *image1 = [NSMutableDictionary dictionary];
     7         image1[@"icon"] = @"hello"; 
     8         image1[@"desc"] = @"这是一张图片的说明";
     9 
    10         NSMutableDictionary *image2 = [NSMutableDictionary dictionary];
    11         image2[@"icon"] = @"hello2";
    12         image2[@"desc"] = @"这是一张图片的说明2";
    13 
    14         //self.imageData = @[image1,image2];//原来的写法
    15         _imageData = @[image1,image2]; //懒加载的写法
    16 
    17     }
    18     return _imageData;
    19 }

    上述get方法不能这样写

    1 -(NSArray *) imageData{
    2     if(self.imageData == nil){
    3         //
    4     }
    5     return self.imageData;
    6 }

    因为这样写会陷入死循环,self.imageData 调用的就是imageData的get方法,所以得写成成员变量"_imageData"这种形式的。

    2.当数据量较多时,可以把数据存储在plist文件中,读取plist文件可以这样来,固定格式,3行。

    //一个bundle就表示一个文件夹,利用mainBundle可以访问手机的任何资源
    NSBundle *bundle = [NSBundle mainBundle];
    //获取plist文件路径,并且这种路径是全路径,而不单单是通过文件名获取
    NSString *path = [bundle pathForResource:@"imageData" ofType:"@"plist"];
    _imageData = [NSArray arrayWithContentOfFile:path];
    //一般含File的路径必须是全路径,而不是和imageNamed的那样,只提供名称就行。

    3.出现这种错误:

    undefined symbols for architecture arm64:
    "_OBJC_CLASS_$_BMKMapView", referenced from:"………………

    这是程序不支持64位命令,有两种修改方法
    ①在target的build settings中的valid Architectures ,将arm64删除,并将Build Active Architecture Only设为NO,即可。

    ②另一种是只需修改Architectures的Architectures改为

    ${ARCHS_STANDARD_32_BIT}

    即可,原来的为

    $(ARCHS_STANDARD)

    4.关于UIButton的点击事件,若将一个btn添加到view里面

    [self addSubView:btn];

    若该button的坐标位于view之外,那么button就接受不到按钮事件。其余的类似

    5.viewWithTag可以根据tag值很快地获取到所需要的view,不过对tag的大小有要求,因为tag值较小的,如0——100为苹果保留所用。

    所以我们自定义时需要把tag设的很大才行,如100000.

    6.userInteractionEnabled是UIView的属性,可以设置视图是否可以接受用户的事件和消息,是否可以跟用户交互。若不想,设置为NO即可。

    如当一个父视图中包含两个子视图a,b。但b被a覆盖了,这样b就不能响应事件,这时若设置

    a.userInteractionEnabled = NO;
    b.userInteractionEnabled = YES;

    这样b就可以接收到消息事件了。

    7.keyWindow是用来接受键盘以及非接触类的信息,而且每个程序中都只能有一个window是keywindow.

    //定义keywindow
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    8.获取UIButton上的文字,可以用

    myButton.titleLabel.text

    9.定义可变数组及初始化

     1 @property(strong, nonatomic) NSMutableArray *allMedicBtn;
     2 
     3 -(NSMutableArray *) allMedicBtn{
     4     if(!_allMedicBtn){
     5       _allMedicBtn = [NSMutableArray array];//第20条笔记讲述这样写的原因
     6     }
     7     return _allMedicBtn; //return self.allMedicBtn
     8     //这里_allMedicBtn相当于self.allMedicBtn,这其实调用的是allMedicBtn的get方法,因此这两种写法是等价的
     9     // self.allMedicBtn 与 _allMedicBtn;
    10 }

    10.[NSMutableArray array]和[[NSMutableArray alloc] init]的区别;

    [NSMutableArray array]相当于[[[NSMutableArray alloc] init] autorelease],autorelease的对象有时会在不用的时候已经release了,后面想用时还得retain一次。

    11.xcode的快捷键

    xocde没有eclipse那样的花括号成对的提示符,只能双击任一花括号,成对的花括号就会以阴影形式出现。
    展开、闭合单一方法,快捷键是 command + option + 左右键,(左键闭合,右键展开)
    要想对所有方法做这些操作,需要加shift,command + option + shift + 左右键

    12.在普通的view中,一些方法可以加载initWithFrame中,如:

    1 -(id)initWithFrame:(CGRect)frame{
    2     self = [super initWithFrame:frame];
    3     if (self) {
    4         //添加选择器
    5         [self setupChoiceView];
    6     }
    7     return self;
    8 }

    但在tableViewCell中,这些方法最好写在initWithStyle中,方便复用。如

    1 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    2 {
    3     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    4         //自定义方法
    5         [self setupFTEHitorySwitchBtn];
    6     }
    7     return self;
    8 }

    13.ios中坐标的定义,当把UIView添加到keyWindow后,view相对的坐标原点就会发生变化,ipad横屏程序中,默认进入主页时的home键是位于左边的,此时的坐标原点应该位于右上角(倒过来看还是左上角)。

    而假如不是加入到keyWindow中,那么坐标原点还是位于左上角。

    而对于keyWindow的定义是在appDelegate中进行的,如

    1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    2       self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    3       self.window.rootViewController = [[LoginViewController alloc]init];
    4       [self.window makeKeyAndVisible];
    5 
    6       return YES;
    7 }

    对于这种问题,则可以这么解决(这是view中添加view)

    ①在view的.h文件中,添加新方法。如

    1 @class HomeViewController; //为下边的方法要使用的类提供接口
    2 @interface MedicineSelectView:UIView
    3 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home;
    4 @end

    ②在view的.m文件中添加自定义的方法,原来的方法不再需要。原来的为

    1 -(id)initWithFrame:(CGRect)frame{
    2     self = [super initWithFrame:frame];
    3     if(self){
    4         [self setupAddMedicineBtn];
    5     }
    6     return self;
    7 }

    此时,上述方法应改为

     1 -(id)initWithFrame:(CGRect) frame andSuperViewController:(HomeViewController *) home{
     2     self = [super initWithFrame:frame];
     3     if(self){
     4         self.home = home; //当然还需要在.m中声明如下
     5         /**
     6         *@property (weak, nonatomic) UIViewController *home;
     7         *因为self.home = home中等号左边的home是指的property的home,等号右边的home是方法中的参数
     8         */
     9         [self setupAddMedicineBtn];
    10     }
    11     return self;
    12 }

    ③在view的.m文件中添加该view时也会发生变化。原来是添加到keyWindow内,如

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubView:self.allMedicineView];

    改变后的方法是将view添加到controller所在的view

    [self.home.view addSubview:self.allMedicineView];

    ④在要将该view添加的controller中,定义该view的大小位置时也需变化。
    原来的是

    MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];

    改变后的是

    MedicineSelectView *medicineView = [[MedicineSelectView alloc] initWithFrame:CGRectMake(0, 0, 300, 44) andSuperViewcontroller:self];
    //self指的是要添加该view的控制器

    14.ipad解决键盘遮住文本框的问题,这种方法较简单,是用来监听文本框的。如有两个文本框userName,passWord(都为UITextField),那么可以这样。

    ①先对两个文本框添加监听事件,这里只以userName为例

    1 [userName addTarget:self action:@selector(textFieldBeginEdit:) forControlEvents:UIControlEventEditingDidBegin];
    2 [userName addTarget:self action:@selector(textFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];

    ②添加具体的移动方法

    //开始编辑时,整体上移
    -(void)textFieldBeginEdit:(UITextField *)textField{
        [self moveView:-230]; 
    }
    
    //结束编辑后,移回原来位置
    -(void)textFieldEndEdit:(UITextField *)textField{
        [self moveView:230];
    }
    
    //view移动的具体方法
    -(void) moveView:(float)move{
        NSTimeInterval animationDuration = 0.3f;
        CGRect frame = self.view.frame;
        frame.origin.x += move; //view的X轴上移
        self.view.frame = frame;
        [UIView beginAnimations:@"2" context:nil];// 2是动画的标识
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
    }

    ③点击其它区域和键盘右下角“隐藏键盘”那个按钮时隐藏键盘,写在viewDidLoad里

    - (void)viewDidLoad {
        
        //点击其他区域关闭键盘
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoard)];
        gesture.numberOfTapsRequired = 1; //确认是单击其他区域,而不是双击等其它操作
        [self.view addGestureRecognizer:gesture];
        
        //该方法特别重要
        //点击键盘右下角“隐藏键盘”那个按钮,关闭键盘并且恢复view的位置
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyBoard) name:UIKeyboardWillHideNotification object:nil];
    }
    
    //隐藏键盘,并且view恢复初始位置
    -(void)hideKeyBoard{
        [userName resignFirstResponder];
        [passWord resignFirstResponder];
        [self resumeView];
    }
    
    //恢复view的原来位置
    -(void)resumeView
    {
        NSTimeInterval animationDuration=0.5f;
        [UIView beginAnimations:@"3" context:nil];
        [UIView setAnimationDuration:animationDuration];
        [UIView commitAnimations];
    }

    15.UITableViewCell的重用,比如表格有100行数据,每页只能显示10行,则当向下划时,第11行出现,第1行消失时,为了节约内存,就要将第1行的cell复用到第11行。代码如下:

    +(instancetype) cellWithTableView:(UITableView *) tableView{
        static NSString *ID = @"MedicineProcessCell";
        MedicineProcessView *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        //表示定义一个cell,在tableView的可重用队列中寻找有MedicineProcessCell标识的UITableViewCell,已进行重用
        //如果队列中有这样的UITableView,则把它赋值给cell;若没有,则返回nil给cell
    
        if(!cell){
            cell = [[MedicineProcessView alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        }
        return cell;
    }
  • 相关阅读:
    bootstrap中nav-fixed-top和nav-static-top的区别
    bootstrap aria
    pixi学习总结
    我的代码需要随时备份,并且保证最新版与别人协同开发?Git与Github一起用啊
    自己本地的代码,如何随时备份?随时找回上个备份?版本控制工具Git啊
    自己的代码,如何跟别人协同开发?用GitHub啊
    函数放到onload里面,在html里面执行函数会报错-----作用域和闭包相关问题
    使用CSS实现空心的向上向下的箭头
    两个input放一行不能对齐
    发现一个h5网站,爱果果
  • 原文地址:https://www.cnblogs.com/Apologize/p/4182084.html
Copyright © 2020-2023  润新知