zoukankan      html  css  js  c++  java
  • 06-联系人管理(xib应用)

    ViewController.h文件中:

    @interface ViewController : UIViewController
    
    - (IBAction)add:(UIBarButtonItem *)sender;
    - (IBAction)remove:(UIBarButtonItem *)sender;
    - (IBAction)deleteClick:(UIButton *)btn;
    
    @property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
    
    
    @end
    View Code

     ViewController.m文件中:

    const int K_ROW_WIDTH = 375;
    const int K_ROW_HEIGHT = 50;
    
    #import "ViewController.h"
    #include "RowView.h"
    
    // 类扩展(class extension),又叫匿名分类
    @interface ViewController ()
    {
        NSArray *_allNames;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        _allNames = @[@"努力", @"奋斗", @"加油", @"动力", @"向上"];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)add:(UIBarButtonItem *)sender {
        // 1.取出最后一个子控件
        UIView *last = [self.view.subviews lastObject];
        CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
        
        // 2.创建一行view
        UIView *row = [self createRowView];
        row.frame = CGRectMake(0, rowY, K_ROW_WIDTH, K_ROW_HEIGHT);
        
        // 3.添加一行到控制器的view中
        [self.view addSubview:row];
        
        // 4.修改删除Item的状态
        _removeItem.enabled = YES;
        
        // 5.添加动画
        CGRect tempFrm = row.frame;
        tempFrm.origin.x = K_ROW_WIDTH;
        row.frame = tempFrm;
        row.alpha = 0;
        
        [UIView animateWithDuration:0.6 animations:^{
            CGRect tempFrm = row.frame;
            tempFrm.origin.x = 0;
            row.frame = tempFrm;
            row.alpha = 1;
        } completion:^(BOOL finished) {
            //
        }];
    }
    
    
    - (UIView *)createRowView
    {
        //通过加载lib进行新建
        NSString *iconName = [NSString stringWithFormat:@"01%d.png", arc4random_uniform(9)];
        NSString *name = _allNames[arc4random_uniform(_allNames.count)];
        UIView *row = [RowView rowViewWithIcon:iconName name:name];
        
        return row;
        
    }
    
    #pragma mark 点击联系人所在行中的“删除”按钮
    - (IBAction)deleteClick:(UIButton *)btn
    {
        
        [UIView animateWithDuration:0.6 animations:^{
            CGRect tempFrm = btn.superview.frame;
            tempFrm.origin.x = K_ROW_WIDTH;
            btn.superview.frame = tempFrm;
            
            btn.superview.alpha = 0;
        } completion:^(BOOL finished) {
            // 获取即将删除的这行在数组中的位置索引
            int startIndex = [self.view.subviews indexOfObject:btn.superview];
            
            [UIView animateWithDuration:0.2 animations:^{
                for (int i = startIndex; i < self.view.subviews.count; ++i) {
                    UIView *child = self.view.subviews[i];
                    CGRect tempFrm = child.frame;
                    tempFrm.origin.y -= K_ROW_HEIGHT + 1;
                    child.frame = tempFrm;
                }
            }];
            
            [btn.superview removeFromSuperview];
            _removeItem.enabled = self.view.subviews.count > 1;
        }];
        
    }
    
    - (IBAction)remove:(UIBarButtonItem *)sender {
        // 1.取出最后一个子控件
        UIView *last = [self.view.subviews lastObject];
        
    //    // 判断最后一个子控件是否为toolbar控件
    //    if ([last isKindOfClass:[UIToolbar class]]) {
    //        return;
    //    }
        
        // 添加动画
        [UIView animateWithDuration:0.6 animations:^{
            CGRect tempFrm = last.frame;
            tempFrm.origin.x = K_ROW_WIDTH;
            last.frame = tempFrm;
            last.alpha = 0;
        } completion:^(BOOL finished) {
            // 2.删除
            [last removeFromSuperview];
            
            // 3.修改删除Item的状态
            _removeItem.enabled = self.view.subviews.count > 1;
        }];
        
    }
    
    
    @end
    View Code

    RowView.xib RowView.h文件中:

    @interface RowView : UIView
    
    + (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name;
    @property (weak, nonatomic) IBOutlet UIButton *iconBtn;
    @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
    
    @end
    View Code

    RowView.xib RowView.m文件中:

    @implementation RowView
    
    +(id)rowViewWithIcon:(NSString *)icon name:(NSString *)name
    {
        RowView *row = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil][0];
        [row.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        row.nameLabel.text = name;
    
        return row;
    }
    
    @end
    View Code

    界面效果:

     一、类扩展(class extension,匿名分类)

     1、格式:

     @interface ViewController ()

     {

     // 成员变量

     }

     // 方法声明

     @end

     2、作用

     1>写在.m文件中

     2>一般用来扩充私有成员变量、@property属性、方法等

     二、随机数的生产

     1、arc4random() 会生成任意正整数和0

     2、arc4random_uniform(100) 会生成0~99的整数(包括0和99)

     三、UIView常用方法

     1、addSubview:(UIView *)child

     添加子控件(最新添加的子控件,

      2、NSArray *subviews

     通过addSubview:方法添加的子控件都会存在于这个数组中

      3、removeFromSuperview

     将控件本身从父控件中移除(控件本身也会从父控件的subviews数组中移除)

      4、(UIView *)viewWithTag:(int)myTag

     * 返回tag值等于mytag的子控件

     * 如果有多个子控件的tag一样,只会返回第一个匹配的子控件(在搜索tag匹配的控件时,也包括控件本身)

      5、UIView *superview

     返回父控件

     四、xib的基本使用

     1.xib和nib是同义词

     2.加载xib文件

     [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil]

     //返回xib文件中,objects下面的所有控件,按顺序装在数组中返回

      3.storyboard和xib的异同

     1>区别

     *stroyboard:描述软件界面,大范围,重量级,比较适合描述整个软件的所有界面

     *xib:描述软件界面,小范围,轻量级,比较适合描述某个小界面(局部界面)

     2>相同点:

     本质都是转成代码(都是xml文件格式)

     4.File‘s Owner的使用步骤:

     1>在xib文件中设置File‘s Owner的calss属性(目的:是在xib中能找到Owner 的方法)

     2>建立File‘s Owner 跟 控件之间的联系(连线)

     3>利用代码加载xib,传递Owner参数(类型一定要匹配)

  • 相关阅读:
    js中FOR循环的陷阱
    Struts2学习第七课 result
    Struts2学习第七课 ActionSupport
    Struts2学习第六课 实现登录登出功能
    Struts2学习第五课 通过和ServletAPI耦合的方式获取WEB资源
    Struts2学习第四课 通过Aware接口获取WEB资源
    子类重写父类的方法
    字节流和字符流
    Java数据库连接库JDBC用到哪种设计模式?
    Java接口
  • 原文地址:https://www.cnblogs.com/smile-smile/p/5236334.html
Copyright © 2011-2022 走看看