代理实现:加载view拿值返回刷新原界面

 //ViewController.m --->>>>

#import "ViewController.h"

#import "MoneyPickerView.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,MoneyPickerView>
{
    UITableView *_tableView;
}
@property (nonatomic,strong)NSString *cellString;
@property (nonatomic,strong)MoneyPickerView *pickerView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];
}

#pragma mark pickerview协议
- (void)sendMoneyString:(NSString *)moneyString{
    _cellString = moneyString;
    [_tableView reloadData];

}

#pragma mark uitableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];//如果重用,把数据加载到重写的cell里,不然刷新会重复加载。
    if (indexPath.row == 0) {
        cell.textLabel.text = @"点击加载数据";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"数据如下";
    }
    if (indexPath.row == 2) {
        cell.detailTextLabel.text = self.cellString;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        _pickerView = [[MoneyPickerView alloc]initWithFrame:CGRectMake(0, 300, CGRectGetWidth(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 300)];
        _pickerView.delegate = self;//遵守协议
        [self.view addSubview:_pickerView];
    }
}


@end

接下来是加载的view:

MoneyPickerView.h
 1 #import <UIKit/UIKit.h>
 2 @class MoneyPickerView;
 3 @protocol MoneyPickerView <NSObject>
 4 - (void)sendMoneyString:(NSString *)moneyString;
 5 @end
 6 @interface MoneyPickerView : UIView
 7 
 8 @property (nonatomic,weak)  id<MoneyPickerView> delegate;
 9 @property (nonatomic,readwrite)  NSInteger currentValue;
10 @end
MoneyPickerView.m
#import "MoneyPickerView.h"
@interface MoneyPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong) UIPickerView *pickerView;
@property (nonatomic,strong)  NSMutableArray *objectArray;
@end
@implementation MoneyPickerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addDayPickerView];
        [self loadPickerDataSource];
        [self loadSelfView];
        [_pickerView selectRow:_currentValue inComponent:0 animated:NO];
    }
    return self;
}

#pragma mark 加载界面
//加载button
-(void)loadSelfView{
    UIButton *canselButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 40, 70)];
    canselButton.backgroundColor = [UIColor clearColor];
    [canselButton setTitle:@"取消" forState:UIControlStateNormal];
    [canselButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [canselButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:canselButton];
    
    UIButton *surelButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.bounds) - 20 - 35, 10, 40, 70)];
    surelButton.backgroundColor = [UIColor clearColor];
    [surelButton setTitle:@"确定" forState:UIControlStateNormal];
    [surelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [surelButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:surelButton];
}

/**
 *  加载数据源
 */
- (void)loadPickerDataSource{
    _objectArray = [[NSMutableArray alloc]init];
    for (NSInteger idx = 0; idx<=60 ;idx++) {
        NSString *value = [NSString stringWithFormat:@"支付宝%ld元",(long)idx];
        [_objectArray addObject:value];
    }
}

/**
 *  初始化pickerview
 */
- (void)addDayPickerView{
    _pickerView = [[UIPickerView alloc]init];
    _currentValue = 0;
    _pickerView.delegate = self;
    [self addSubview:_pickerView];
}

#pragma mark 自定义方法
/**
 *  确定按钮
 *
 *  @param sender button事件
 */
- (void)sureButtonPressed:(UIButton *)sender{
//    if ([_delegate respondsToSelector:@selector(sendMoneyString:)]) {
//    }
    [_delegate sendMoneyString:_objectArray[_currentValue]];
    [self removeFromSuperview];
}

/**
 *  取消按钮
 *
 *  @param sender
 */
- (void)cancelButtonPressed:(UIButton *)sender{
    [self removeFromSuperview];
}

#pragma mark pickerView协议方法
//返回的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
//选择的行数
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _currentValue = row;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _objectArray.count;
}

//没列的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_objectArray objectAtIndex:row];
}


@end

 

 
posted @ 2015-09-06 21:11  梦影随风  阅读(152)  评论(0编辑  收藏  举报