zoukankan      html  css  js  c++  java
  • iOS 开发技巧

    一、收起键盘

    在UIViewController中收起键盘,经常看到一些写法,如:

    - (void)closeKeyBoard
    {
        if ([_userTextField isFirstResponder]) {
            [_userTextField resignFirstResponder];
        }
        else if ([_pwdTextField isFirstResponder])
        {
            [_pwdTextField resignFirstResponder];
        }
    }

    除了调用相应控件的resignFirstResponder方法外,还有另外三种方法:

    1.重载UIViewController 中的 touchesBegan 方法,然后在里面执行[self.view endEditing:YES];

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        [self.view endEditing:YES];
    }

    2.当获得当前UIViewController比较困难的时候,直接执行

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

    3.直接执行

    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    4.如果你的controller是一个tableView或者scrollview,可以通过添加手势的方式来处理

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
        // 设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
        tapGestureRecognizer.cancelsTouchesInView = NO;
        // 将触摸事件添加到当前view
        [_tableView addGestureRecognizer:tapGestureRecognizer];
    - (void)keyboardHide:(UITapGestureRecognizer *)tap
    {
        [self.view endEditing:YES];
    }

    二、NSJSONSerialization比NSKeyedArchiver更好

    在选择持久化方案时,系统提供NSJSONSerialization比NSKeyedArchiver在效率和体积上都更优。经过简单测试,NSJSONSerialization比NSKeyedArchiver快了7倍,而且序列化之后的体积是NSKeyedArchiver的一半。

    网上也有更详细的测试证明了该说法:https://github.com/randomsequence/nsserialisationtests

    未完会持续补充~~~~

  • 相关阅读:
    flask项目--认证方案Json Web Token(JWT)
    分布式设计-集群
    分布式设计-哨兵
    分布式设计--数据库主从
    leetcode 221 Maximal Square
    LeetCode222 Count Complete Tree Nodes
    在windows 、linux下读取目录下所有文件名
    leetcode 229 Majority Element II
    leetcode 233 Number of Digit One
    leetcode 238 Product of Array Except Self
  • 原文地址:https://www.cnblogs.com/jys509/p/5466039.html
Copyright © 2011-2022 走看看