IOS_UI_day4_xib

H:/IOS_UI/day4-00-联系人管理代码说明.m
01-联系人管理
1> 实现方式:纯代码
2> 实现功能:添加\删除一行、能获得每行的姓名

01-联系人管理 - 2
1> 实现方式:纯代码
2> 实现功能:添加\删除一行、能获得每行的姓名、每行都有个删除按钮

01-联系人管理 - 3
1> 实现方式:代码 + xib
2> 实现功能:添加\删除一行、能获得每行的姓名、每行都有个删除按钮(通过代码监听每行的删除按钮)

01-联系人管理 - 4
1> 实现方式:代码 + xib
2> 实现功能:添加\删除一行、能获得每行的姓名、每行都有个删除按钮(通过拖线监听每行的删除按钮,MJViewController监听按钮点击)

H:/IOS_UI/day4-01-联系人管理-MJViewController.h
//
//  MJViewController.h
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
{

}
// 添加
- (IBAction)add:(UIBarButtonItem *)sender;
// 删除
- (IBAction)remove:(UIBarButtonItem *)sender;


@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
@end

H:/IOS_UI/day4-01-联系人管理-MJViewController.m
//
//  MJViewController.m
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

#define kDuration 0.5
#define kRowH 50
#define kNameTag 10

// 类扩展(class extension,匿名分类)
@interface MJViewController ()
{
    NSArray *_allNames;
}
@end

@implementation MJViewController

#pragma mark 控制器的view加载完毕的时候调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _allNames = @[@"西门庆", @"东门庆", @"北门庆", @"南门庆", @"中门庆"];
}

#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *)sender {
    // 0.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    // 这行的Y = 最后一个子控件的Y + 最后一个子控件的高度 + 间距
    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
    
    // 1.创建一行
    UIView *rowView = [self createRowView];
    
    // 2.添加一行到控制器的view中
    [self.view addSubview:rowView];
    
    // 3.让删除item有效
    _removeItem.enabled = YES;
    
    // 4.执行动画
    rowView.frame = CGRectMake(320, rowY, 320, kRowH);
    rowView.alpha = 0;
    
    // 4.1.开始动画
    [UIView animateWithDuration:kDuration animations:^{
        rowView.frame = CGRectMake(0, rowY, 320, kRowH);
        rowView.alpha = 1;
    }];
}

#pragma mark 创建一行
- (UIView *)createRowView
{
    // 1.创建红色的父控件
    UIView *rowView = [[UIView alloc] init];
    rowView.backgroundColor = [UIColor redColor]; // 红色

    // 2.添加名字(标签)
    UILabel *name = [[UILabel alloc] init];
    name.frame = CGRectMake(0, 0, 320, kRowH);
//    name.center = CGPointxMake(160, kRowH * 0.5);
    int nameIndex = arc4random_uniform(_allNames.count);
    name.text = _allNames[nameIndex];
    name.backgroundColor = [UIColor clearColor];
    // 设置文字居中
    name.textAlignment = NSTextAlignmentCenter;
    
    name.tag = kNameTag;
    
    [rowView addSubview:name];
    
    // 3.添加头像
//    UIButton *icon = [[UIButton alloc] init];
    UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
    icon.frame = CGRectMake(20, 0, 50, kRowH);
    
    // 随机产生文件名
//    int ramdomIndex = arc4random() % 9;
    int randomIndex = arc4random_uniform(9);
    NSString *iconName = [NSString stringWithFormat:@"01%d.png", randomIndex];
    // 设置图片
    [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    
    // 添加监听器
    [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [rowView addSubview:icon];
    return rowView;
}

#pragma mark 监听按钮点击
- (void)iconClick:(UIButton *)btn
{
    // 1.取得按钮的父控件(因为label和btn处在同一个父控件中)
//    btn.superview;
    // 2.获得文本标签
    UILabel *label = (UILabel *)[btn.superview viewWithTag:kNameTag];
    
    // 3.打印
    NSLog(@"名字是:%@", label.text);
}

#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *)sender {
    // 1.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = last.frame;
        tempF.origin.x = 320;
        last.frame = tempF;
        
        last.alpha = 0;
    } completion:^(BOOL finished) {
        [last removeFromSuperview];
        
        // 剩下的子控件个数 > 1就能够点击“删除”
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}
@end

H:/IOS_UI/day4-02-联系人管理-MJViewController.h
//
//  MJViewController.h
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
{

}
// 添加
- (IBAction)add:(UIBarButtonItem *)sender;
// 删除
- (IBAction)remove:(UIBarButtonItem *)sender;


@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
@end

H:/IOS_UI/day4-02-联系人管理-MJViewController.m
//
//  MJViewController.m
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

#define kDuration 0.5
#define kRowH 50
#define kNameTag 10

// 类扩展(class extension,匿名分类)
@interface MJViewController ()
{
    NSArray *_allNames;
}
@end

@implementation MJViewController

#pragma mark 控制器的view加载完毕的时候调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _allNames = @[@"西门庆", @"东门庆", @"北门庆", @"南门庆", @"中门庆"];
}

#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *)sender {
    // 0.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    // 这行的Y = 最后一个子控件的Y + 最后一个子控件的高度 + 间距
    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
    
    // 1.创建一行
    UIView *rowView = [self createRowView];
    
    // 2.添加一行到控制器的view中
    [self.view addSubview:rowView];
    
    // 3.让删除item有效
    _removeItem.enabled = YES;
    
    // 4.执行动画
    rowView.frame = CGRectMake(320, rowY, 320, kRowH);
    rowView.alpha = 0;
    
    // 4.1.开始动画
    [UIView animateWithDuration:kDuration animations:^{
        rowView.frame = CGRectMake(0, rowY, 320, kRowH);
        rowView.alpha = 1;
    }];
}

#pragma mark 创建一行
- (UIView *)createRowView
{
    // 1.创建红色的父控件
    UIView *rowView = [[UIView alloc] init];
    rowView.backgroundColor = [UIColor redColor]; // 红色

    // 2.添加名字(标签)
    UILabel *name = [[UILabel alloc] init];
    name.frame = CGRectMake(0, 0, 320, kRowH);
//    name.center = CGPointxMake(160, kRowH * 0.5);
    int nameIndex = arc4random_uniform(_allNames.count);
    name.text = _allNames[nameIndex];
    name.backgroundColor = [UIColor clearColor];
    // 设置文字居中
    name.textAlignment = NSTextAlignmentCenter;
    name.tag = kNameTag; // 绑定标记
    [rowView addSubview:name];
    
    // 3.添加头像
//    UIButton *icon = [[UIButton alloc] init];
    UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
    icon.frame = CGRectMake(20, 0, 50, kRowH);
    
    // 随机产生文件名
//    int ramdomIndex = arc4random() % 9;
    int randomIndex = arc4random_uniform(9);
    NSString *iconName = [NSString stringWithFormat:@"01%d.png", randomIndex];
    // 设置图片
    [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    // 添加监听器
    [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
    [rowView addSubview:icon];
    
    // 4.删除按钮
    UIButton *delete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    delete.frame = CGRectMake(250, 10, 60, 35);
    [delete setTitle:@"删除" forState:UIControlStateNormal];
    [delete addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    [rowView addSubview:delete];
    
    return rowView;
}

#pragma mark 监听删除按钮点击
- (void)deleteClick:(UIButton *)btn
{
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = btn.superview.frame;
        tempF.origin.x = 320;
        btn.superview.frame = tempF;
        
        btn.superview.alpha = 0;
    } completion:^(BOOL finished) {
        // 1.获得即将删除的这行在数组中的位置
        int startIndex = [self.view.subviews indexOfObject:btn.superview];
        
        // 2.删除当前行
        [btn.superview removeFromSuperview];
        
        [UIView animateWithDuration:0.3 animations:^{
            // 3.遍历后面的子控件
            for (int i = startIndex; i<self.view.subviews.count; i++) {
                UIView *child = self.view.subviews[i];
                CGRect tempF = child.frame;
                tempF.origin.y -= kRowH + 1;
                child.frame = tempF;
            }
        }];
        
        // 4.判断垃圾桶
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}

#pragma mark 监听头像按钮点击
- (void)iconClick:(UIButton *)btn
{
    // 1.取得按钮的父控件(因为label和btn处在同一个父控件中)
//    btn.superview;
    // 2.获得文本标签
    UILabel *label = (UILabel *)[btn.superview viewWithTag:kNameTag];
    
    // 3.打印
    NSLog(@"名字是:%@", label.text);
}

#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *)sender {
    // 1.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = last.frame;
        tempF.origin.x = 320;
        last.frame = tempF;
        
        last.alpha = 0;
    } completion:^(BOOL finished) {
        [last removeFromSuperview];
        
        // 剩下的子控件个数 > 1就能够点击“删除”
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}
@end

H:/IOS_UI/day4-03-联系人管理-MJViewController.h
//
//  MJViewController.h
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
{

}
// 添加
- (IBAction)add:(UIBarButtonItem *)sender;
// 删除
- (IBAction)remove:(UIBarButtonItem *)sender;


@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
@end

H:/IOS_UI/day4-03-联系人管理-MJViewController.m
//
//  MJViewController.m
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

#define kDuration 0.5
#define kRowH 50
#define kNameTag 10

// 类扩展(class extension,匿名分类)
@interface MJViewController ()
{
    NSArray *_allNames;
}
@end

@implementation MJViewController

#pragma mark 控制器的view加载完毕的时候调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _allNames = @[@"西门庆", @"东门庆", @"北门庆", @"南门庆", @"中门庆"];
}

#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *)sender {
    // 0.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    // 这行的Y = 最后一个子控件的Y + 最后一个子控件的高度 + 间距
    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
    
    // 1.创建一行
    UIView *rowView = [self createRowView];
    
    // 2.添加一行到控制器的view中
    [self.view addSubview:rowView];
    
    // 3.让删除item有效
    _removeItem.enabled = YES;
    
    // 4.执行动画
    rowView.frame = CGRectMake(320, rowY, 320, kRowH);
    rowView.alpha = 0;
    
    // 4.1.开始动画
    [UIView animateWithDuration:kDuration animations:^{
        rowView.frame = CGRectMake(0, rowY, 320, kRowH);
        rowView.alpha = 1;
    }];
}

#pragma mark 创建一行(从xib中加载一行的view)
// xib == nib
- (UIView *)createRowView
{
    // 1.加载RowView.xib文件,创建Objects下面的所有控件,并且按顺序装到数组中返回
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil];
    
    // 2.取出一行的红色view
    UIView *rowView = views[0];
    
    // 3.设置头像
    UIButton *icon = (UIButton *)[rowView viewWithTag:1];
    NSString *iconName = [NSString stringWithFormat:@"01%d.png", arc4random_uniform(9)];
    [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    
    // 4.设置姓名
    UILabel *name = (UILabel *)[rowView viewWithTag:2];
    name.text = _allNames[arc4random_uniform(_allNames.count)];
    
    // 5.监听删除按钮
    UIButton *delete = (UIButton *)[rowView viewWithTag:3];
    [delete addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    return rowView;
}

#pragma mark 监听删除按钮点击
- (void)deleteClick:(UIButton *)btn
{
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = btn.superview.frame;
        tempF.origin.x = 320;
        btn.superview.frame = tempF;
        
        btn.superview.alpha = 0;
    } completion:^(BOOL finished) {
        // 1.获得即将删除的这行在数组中的位置
        int startIndex = [self.view.subviews indexOfObject:btn.superview];
        
        // 2.删除当前行
        [btn.superview removeFromSuperview];
        
        [UIView animateWithDuration:0.3 animations:^{
            // 3.遍历后面的子控件
            for (int i = startIndex; i<self.view.subviews.count; i++) {
                UIView *child = self.view.subviews[i];
                CGRect tempF = child.frame;
                tempF.origin.y -= kRowH + 1;
                child.frame = tempF;
            }
        }];
        
        // 4.判断垃圾桶
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}

#pragma mark 监听头像按钮点击
- (void)iconClick:(UIButton *)btn
{
    // 1.取得按钮的父控件(因为label和btn处在同一个父控件中)
//    btn.superview;
    // 2.获得文本标签
    UILabel *label = (UILabel *)[btn.superview viewWithTag:kNameTag];
    
    // 3.打印
    NSLog(@"名字是:%@", label.text);
}

#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *)sender {
    // 1.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = last.frame;
        tempF.origin.x = 320;
        last.frame = tempF;
        
        last.alpha = 0;
    } completion:^(BOOL finished) {
        [last removeFromSuperview];
        
        // 剩下的子控件个数 > 1就能够点击“删除”
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}
@end

H:/IOS_UI/day4-04-联系人管理-MJViewController.h
//
//  MJViewController.h
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
{

}
// 添加
- (IBAction)add:(UIBarButtonItem *)sender;
// 删除
- (IBAction)remove:(UIBarButtonItem *)sender;

// 监听每一行的删除按钮
- (IBAction)deleteClick:(UIButton *)btn;


@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
@end

H:/IOS_UI/day4-04-联系人管理-MJViewController.m
//
//  MJViewController.m
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"

#define kDuration 0.5
#define kRowH 50
#define kNameTag 10

// 类扩展(class extension,匿名分类)
@interface MJViewController ()
{
    NSArray *_allNames;
}
@end

@implementation MJViewController

#pragma mark 控制器的view加载完毕的时候调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _allNames = @[@"西门庆", @"东门庆", @"北门庆", @"南门庆", @"中门庆"];
}

#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *)sender {
    // 0.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    // 这行的Y = 最后一个子控件的Y + 最后一个子控件的高度 + 间距
    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
    
    // 1.创建一行
    UIView *rowView = [self createRowView];
    
    // 2.添加一行到控制器的view中
    [self.view addSubview:rowView];
    
    // 3.让删除item有效
    _removeItem.enabled = YES;
    
    // 4.执行动画
    rowView.frame = CGRectMake(320, rowY, 320, kRowH);
    rowView.alpha = 0;
    
    // 4.1.开始动画
    [UIView animateWithDuration:kDuration animations:^{
        rowView.frame = CGRectMake(0, rowY, 320, kRowH);
        rowView.alpha = 1;
    }];
}

#pragma mark 创建一行(从xib中加载一行的view)
// xib == nib
- (UIView *)createRowView
{
    // 1.加载RowView.xib文件,创建Objects下面的所有控件,并且按顺序装到数组中返回
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil];
    
    // 2.取出一行的红色view
    UIView *rowView = views[0];
    
    // 3.设置头像
    UIButton *icon = (UIButton *)[rowView viewWithTag:1];
    NSString *iconName = [NSString stringWithFormat:@"01%d.png", arc4random_uniform(9)];
    [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    
    // 4.设置姓名
    UILabel *name = (UILabel *)[rowView viewWithTag:2];
    name.text = _allNames[arc4random_uniform(_allNames.count)];
    return rowView;
}

#pragma mark 监听删除按钮点击
- (void)deleteClick:(UIButton *)btn
{
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = btn.superview.frame;
        tempF.origin.x = 320;
        btn.superview.frame = tempF;
        
        btn.superview.alpha = 0;
    } completion:^(BOOL finished) {
        // 1.获得即将删除的这行在数组中的位置
        int startIndex = [self.view.subviews indexOfObject:btn.superview];
        
        // 2.删除当前行
        [btn.superview removeFromSuperview];
        
        [UIView animateWithDuration:0.3 animations:^{
            // 3.遍历后面的子控件
            for (int i = startIndex; i<self.view.subviews.count; i++) {
                UIView *child = self.view.subviews[i];
                CGRect tempF = child.frame;
                tempF.origin.y -= kRowH + 1;
                child.frame = tempF;
            }
        }];
        
        // 4.判断垃圾桶
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}

#pragma mark 监听头像按钮点击
- (void)iconClick:(UIButton *)btn
{
    // 1.取得按钮的父控件(因为label和btn处在同一个父控件中)
//    btn.superview;
    // 2.获得文本标签
    UILabel *label = (UILabel *)[btn.superview viewWithTag:kNameTag];
    
    // 3.打印
    NSLog(@"名字是:%@", label.text);
}

#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *)sender {
    // 1.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = last.frame;
        tempF.origin.x = 320;
        last.frame = tempF;
        
        last.alpha = 0;
    } completion:^(BOOL finished) {
        [last removeFromSuperview];
        
        // 剩下的子控件个数 > 1就能够点击“删除”
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}
@end

H:/IOS_UI/day4-05-联系人管理-MJViewController.h
//
//  MJViewController.h
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
{

}
// 添加
- (IBAction)add:(UIBarButtonItem *)sender;
// 删除
- (IBAction)remove:(UIBarButtonItem *)sender;

// 监听每一行的删除按钮
- (IBAction)deleteClick:(UIButton *)btn;


@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;
@end

H:/IOS_UI/day4-05-联系人管理-MJViewController.m
//
//  MJViewController.m
//  04-联系人管理
//
//  Created by apple on 13-11-24.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "RowView.h"

#define kDuration 0.5
#define kRowH 50
#define kNameTag 10

// 类扩展(class extension,匿名分类)
@interface MJViewController ()
{
    NSArray *_allNames;
}
@end

@implementation MJViewController

#pragma mark 控制器的view加载完毕的时候调用一次
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _allNames = @[@"西门庆", @"东门庆", @"北门庆", @"南门庆", @"中门庆"];
}

#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *)sender {
    // 0.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    // 这行的Y = 最后一个子控件的Y + 最后一个子控件的高度 + 间距
    CGFloat rowY = last.frame.origin.y + last.frame.size.height + 1;
    
    // 1.创建一行
    UIView *rowView = [self createRowView];
    
    // 2.添加一行到控制器的view中
    [self.view addSubview:rowView];
    
    // 3.让删除item有效
    _removeItem.enabled = YES;
    
    // 4.执行动画
    rowView.frame = CGRectMake(320, rowY, 320, kRowH);
    rowView.alpha = 0;
    
    // 4.1.开始动画
    [UIView animateWithDuration:kDuration animations:^{
        rowView.frame = CGRectMake(0, rowY, 320, kRowH);
        rowView.alpha = 1;
    }];
}

#pragma mark 创建一行(从xib中加载一行的view)
// xib == nib
- (UIView *)createRowView
{
    NSString *icon = [NSString stringWithFormat:@"01%d.png", arc4random_uniform(9)];
    NSString *name = _allNames[arc4random_uniform(_allNames.count)];
    
    RowView *rowView = [RowView rowViewWithIcon:icon name:name];
    
    return rowView;
}

#pragma mark 监听删除按钮点击
- (void)deleteClick:(UIButton *)btn
{
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = btn.superview.frame;
        tempF.origin.x = 320;
        btn.superview.frame = tempF;
        
        btn.superview.alpha = 0;
    } completion:^(BOOL finished) {
        // 1.获得即将删除的这行在数组中的位置
        int startIndex = [self.view.subviews indexOfObject:btn.superview];
        
        // 2.删除当前行
        [btn.superview removeFromSuperview];
        
        [UIView animateWithDuration:0.3 animations:^{
            // 3.遍历后面的子控件
            for (int i = startIndex; i<self.view.subviews.count; i++) {
                UIView *child = self.view.subviews[i];
                CGRect tempF = child.frame;
                tempF.origin.y -= kRowH + 1;
                child.frame = tempF;
            }
        }];
        
        // 4.判断垃圾桶
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}

#pragma mark 监听头像按钮点击
- (void)iconClick:(UIButton *)btn
{
    // 1.取得按钮的父控件(因为label和btn处在同一个父控件中)
//    btn.superview;
    // 2.获得文本标签
    UILabel *label = (UILabel *)[btn.superview viewWithTag:kNameTag];
    
    // 3.打印
    NSLog(@"名字是:%@", label.text);
}

#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *)sender {
    // 1.取出最后一个子控件
    UIView *last = [self.view.subviews lastObject];
    
    [UIView animateWithDuration:kDuration animations:^{
        CGRect tempF = last.frame;
        tempF.origin.x = 320;
        last.frame = tempF;
        
        last.alpha = 0;
    } completion:^(BOOL finished) {
        [last removeFromSuperview];
        
        // 剩下的子控件个数 > 1就能够点击“删除”
        _removeItem.enabled = self.view.subviews.count > 1;
    }];
}
@end

H:/IOS_UI/day4-05-联系人管理-RowView.h
//
//  RowView.h
//  01-联系人管理
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RowView : UIView
+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name;

@property (nonatomic, weak) IBOutlet UIButton *iconBtn;
@property (nonatomic, weak) IBOutlet UILabel *nameLabel;

@end

H:/IOS_UI/day4-05-联系人管理-RowView.m
//
//  RowView.m
//  01-联系人管理
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "RowView.h"

@implementation RowView

+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name
{
    RowView *view = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil][0];
    
    // 1.设置图标
//    UIButton *iconBtn = (UIButton *)[view viewWithTag:1];
    [view.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    
    // 2.设置姓名
//    UILabel *nameLabel = (UILabel *)[view viewWithTag:2];
    view.nameLabel.text = name;
    
    return view;
}



@end

H:/IOS_UI/day4-06-xib的owner-Dog.h
//
//  Dog.h
//  02-xib的owner
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Dog : NSObject

@property (nonatomic, assign) int age;

- (IBAction)btnClick;
@end

H:/IOS_UI/day4-06-xib的owner-Dog.m
//
//  Dog.m
//  02-xib的owner
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Dog.h"

@implementation Dog
- (void)btnClick
{
    NSLog(@"%d岁的狗在汪汪.....", _age);
}

/*
- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options
{

    //	 解析HisView.xib文件 

    //   创建Objects下面的所有对象
    // 创建view
    UIView *blue = [[UIView alloc] init];
    blue.backgroundColor = [UIColor blueColor];
    //....设置其他属性

    // 创建按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"我是按钮" forState:UIControlStateNormal];
    [btn addTarget:owner action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
//    ....设置其他属性
    
    [blue addSubview:btn];
    
    // 创建文本输入框
    UITextField *field = [[UITextField alloc] init];
    // ... 设置其他属性
    
    return @[ blue, field];
}
 
 */

@end

H:/IOS_UI/day4-06-xib的owner-MJViewController.h
//
//  MJViewController.h
//  02-xib的owner
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController

- (IBAction)leftClick;
- (IBAction)rightClick;

@end

H:/IOS_UI/day4-06-xib的owner-MJViewController.m
//
//  MJViewController.m
//  02-xib的owner
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "Dog.h"

@interface MJViewController ()
{
    Dog *_d1;
    Dog *_d2;
}
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.加载xib文件
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
    
    UIView *greenView = array[0];
    
    // 2.添加到控制器的view里面去
    [self.view addSubview:greenView];
    
    // 3.
//    UIButton *left = (UIButton *)[greenView viewWithTag:10];
//    [left addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
//    
//    UIButton *right = (UIButton *)[greenView viewWithTag:20];
//    [right addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    
    _d1 = [[Dog alloc] init];
    _d1.age = 10;
    
    _d2 = [[Dog alloc] init];
    _d2.age = 20;
    
    // 添加蓝色的view
    UIView *blueView = [[NSBundle mainBundle] loadNibNamed:@"HisView" owner:_d2 options:nil][0];
    blueView.center = CGPointMake(160, 200);
    [self.view addSubview:blueView];
}

- (void)leftClick
{
    NSLog(@"left------");
}

- (void)rightClick
{
    NSLog(@"right------");
}
@end

H:/IOS_UI/day4-07-xib的重用-MJViewController.h
//
//  MJViewController.h
//  03-xib的重用
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController

@end

H:/IOS_UI/day4-07-xib的重用-MJViewController.m
//
//  MJViewController.m
//  03-xib的重用
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "RowView.h"

@interface MJViewController ()

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    RowView *row = [RowView rowViewWithIcon:@"017.png" name:@"哈哈哈"];
    
    [self.view addSubview:row];
}
@end

H:/IOS_UI/day4-07-xib的重用-RowView.h
//
//  RowView.h
//  01-联系人管理
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RowView : UIView
+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name;


@property (nonatomic, weak) IBOutlet UIButton *iconBtn;

@property (nonatomic, weak) IBOutlet UILabel *nameLabel;

@end

H:/IOS_UI/day4-07-xib的重用-RowView.m
//
//  RowView.m
//  01-联系人管理
//
//  Created by apple on 13-11-25.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "RowView.h"

@implementation RowView

+ (id)rowViewWithIcon:(NSString *)icon name:(NSString *)name
{
    RowView *view = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil][0];
    
    // 1.设置图标
//    UIButton *iconBtn = (UIButton *)[view viewWithTag:1];
    [view.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    
    // 2.设置姓名
//    UILabel *nameLabel = (UILabel *)[view viewWithTag:2];
    view.nameLabel.text = name;
    
    return view;
}



@end

你可能感兴趣的:(ios,UI,xib)