• IOS工作笔记(六)


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

    1.NSArray中使用firstObject和lastObject的好处在于,当数组为空时,返回nil,而不会报错。

    当数组为空,使用myArray[0]时,会报错。

    2.UILabel和UIImageView都可以添加点击事件,以UIlabel为例。

    1 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSpecialWith)];
    2 //必须设置userInteractionEnabled为yes,否则无效
    3 myLabel.userInteractionEnabled = YES;
    4 [myLabel addGestureRecognizer:tapGesture];

    3.在普通的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 }

    4.view的模态显示,效果类似于jsp的alert弹窗,如点击A类按钮,弹出B类的界面,则可以在

    A类的点击方法中写:

    1 UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:BController];
    2 //设置弹窗的模式
    3 navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    4 [self presentViewController:navigationController animated:YES completion:nil];

    然后在弹窗页面想返回A类,那么需要在B类中添加返回方法,

     1 // 设置返回按钮
     2 -(void)setupReturnBackBtn{
     3     UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"Return" style:UIBarButtonItemStyleDone target:self action:@selector(backToPrevious)];
     4     [backBarBtn setTintColor:[UIColor whiteColor]];
     5     //定义barItem的左边的bar
     6     self.navigationItem.leftBarButtonItem = backBarBtn;
     7 }
     8 
     9 //返回上一页
    10 -(void)backToPrevious{
    11     [self dismissViewControllerAnimated:YES completion:nil];
    12     //这里不能用[self popToRootViewControllerAnimated:YES]
    13 }

    5.UITableView的刷新,分为局部刷新和全部刷新。

    1 //刷新tableView的所有cell
    2 [self.tableView reloadData]; 
    3 
    4 //刷新单个cell,如下边的就是刷新第1个section的第1个cell
    5 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    6 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

    此时需要注意的是,下标不能越界。

    6.respondsToSelector用来判断对象是否有某个方法,这是被封装在selector对象里传递,一般用在代理中。

    如在cell中,点击按钮通过push方法跳往另一个controller,则只能通过代理,因为在cell里边没有push这个方法。
    在cell的.h中

    1 @protocol MSFouthScatterDelegate <NSObject>
    2 -(void)showFouthScatterView;
    3 @end
    4 
    5 @interface MSFouthScatterCell : UITableViewCell
    6 
    7 @property(nonatomic,weak) id<MSFouthScatterDelegate> delegate;
    8 
    9 @end

    在cell的.m中,

    [btn addTarget:self action:@selector(clickScatterBtn) forControlEvents:UIControlEventTouchUpInside];
    
    //需要注意的是,这两个地方的方法名必须相同,否则报错。

    -(void)clickScatterBtn{
        if ([self.delegate respondsToSelector:@selector(showFouthScatterView)]) {
            [self.delegate showFouthScatterView];
        }
    }

    在controller的.m中

     1 @interface MSSalesFourthController ()<MSFouthScatterDelegate>
     2 
     3 @end
     4 @implementation MSSalesFourthController
     5 //跳往另一个controller
     6 - (void)showFouthScatterView{
     7     MSFouthScatterController *vc = [[MSFouthScatterController alloc] init];
     8     [self.navigationController pushViewController:vc animated:YES];
     9 }
    10 @end

    这样即可。

    7.做登录界面时,遇到一个问题。往UIImageView添加输入用户名和密码的UITextField时,textField不会响应点击,即不会弹出键盘。

    此时只要设置imageView的userInteractionEnabled = YES 即可。
    原先处理时是这样做

    imageView.userInteractionEnabled = NO;
    userName.userInteractionEnabled = YES;
    userName.enabled = YES;
    password.userInteractionEnabled = YES
    password.enabled = YES;

    但没有效果,其实只需要一行代码就够,其余可删除。

    imageView.userInteractionEnabled = YES;

    8.tableView如何不显示空白cell?如下,不想让下边空白的显示

    只需一行代码

    self.tableView.tableFooterView = [[UIView alloc]init];

    结果如下:

    当然,也可以用麻烦点的方法。在controller中

    1 -(void)viewDidLoad{
    2     [super viewDidLoad];
    3 
    4     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    5 }

    然后在自定义的cell中手动添加虚线。

     1 - (void)layoutSubviews
     2 {
     3     [super layoutSubviews];
     4     
     5     // 手动添加线
     6     UIView *view = [[UIView alloc] init];
     7     view.frame = CGRectMake(10, self.height -1, self.width - 20, 1);
     8     view.backgroundColor = [UIColor lightGrayColor];
     9     [self.contentView addSubview:view];
    10 }

    9.怎么改变uitextfield placeholder的颜色和位置?

    1 - (void) drawPlaceholderInRect:(CGRect)rect {
    2     [[UIColor blueColor] setFill];
    3     [self.placeholder drawInRect:rect withFont:self.font lineBreakMode:UILineBreakModeTailTruncation alignment:self.textAlignment];
    4 }

    10.button的textLabel默认情况下是填充整个button的,这个可以由下面确定

    myBtn.backgroundColor = [UIColor cyanColor]; 
    myBtn.textLabel.backgroundColor = [UIColor redColor];

    可以发现整个button会变成红色。
    当button的背景图片看起来有些小时,若想使文字放到背景图片内,就可以使用内边距了。
    相当于减少textLabel的大小。默认情况下textLabel的内边距就是(0,0,0,0).设置内边距可以用:

    myBtn.contentEdgeInsets = UIEdgeInsetsMake(20,20,20,20);

    效果就是textLabel分别距button的上下左右都是20point。

  • 相关阅读:
    静态代理模式
    当对象或属性为空时,如何安全的给默认值
    linux抓取日志
    BigDecimal进行比较
    int与Double类型转换
    通过配置文件的key得值
    ObjectNode ArrayNode JsonNode
    java.lang.NoSuchFieldError报错解决方案
    BigDecimal.setScale 处理java小数点
    Jmeter实现压力测试(多并发测试)
  • 原文地址:https://www.cnblogs.com/Apologize/p/4308911.html
Copyright © 2020-2023  润新知