UI_协议传值

来源:互联网 发布:什么是数据库的安全性 编辑:程序博客网 时间:2024/06/07 11:27

#import "CellView.h"


#define KScrollWidth [UIScreen mainScreen].bounds.size.width

#define kScrollHeight [UIScreen mainScreen].bounds.size.height

@implementation CellView


//用懒加载的方式(延迟加载)属性的getter方法

-(UIImageView *)headImageView{

    if (!_headImageView) {

        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 60, 60)];

        _headImageView.layer.cornerRadius = 30;

        _headImageView.layer.masksToBounds = YES;

        [self addSubview:_headImageView];

    }

    return _headImageView;

}


-(UILabel *)nameLabel{

    if (!_nameLabel) {

        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 15, KScrollWidth-90, 30)];

        [self addSubview:_nameLabel];

    }

    return _nameLabel;

}


-(UILabel *)phoneLabel{

    if (!_phoneLabel) {

        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 50, KScrollWidth-90, 30)];

        [self addSubview:_phoneLabel];

    }

    return _phoneLabel;

}


//cellView添加底部的分割线

-(instancetype)initWithFrame:(CGRect)frame{

        //如果frame的大小比我们所需要的最小值还要小,那么就需要更改frame的大小

        //高度的保险措施

    if (frame.size.height <121) {//最小限制

        frame.size.height = 121;

    }else if(frame.size.height >200){ //最大限制

        frame.size.height = 200;

    }

        //宽度的保险措施

    frame.size.width = KScrollWidth;

    

    self = [super initWithFrame:frame];

    if (self) {

        UILabel *cuttingLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(frame), CGRectGetWidth(frame), 1)];

        cuttingLabel.backgroundColor = [UIColor redColor];

        [self addSubview:cuttingLabel];

    }

    return self;

}




@end



#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   

    RootViewController *rootVC = [[RootViewController alloc]init];

    

    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];

    

    self.window.rootViewController = navC;

    


    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

@end




#import "RootViewController.h"

#import "UserinfoViewController.h"

#import "CellView.h"

@interface RootViewController ()<UserinfoViewControllerDelegate>

//属性

@property(nonatomic,retain)UITextField *titleField;

@end

@implementation RootViewController

/*

    //懒加载

-(UITextField *)titleField{


    if (!_titleField) {

        _titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];

        _titleField.center = self.view.center;

        _titleField.placeholder = @"请输入标题";

        [self.view addSubview:_titleField];

    }

    return _titleField;

}

*/





- (void)viewDidLoad {

    [super viewDidLoad];

 /*

    self.navigationItem.title = @"用户列表";

   

        //右边按钮

    self.titleField.placeholder = @"请输入标题";


    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"用户姓名" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];


    self.navigationItem.rightBarButtonItem = rightButton;

  */

    

    

//初始化

    CellView *cellView = [[CellView alloc]initWithFrame:CGRectMake(0, 80, 0, 0)];

    cellView.headImageView.image = [UIImage imageNamed:@"Two.png"];

    cellView.nameLabel.text = @"HanOBa";

    cellView.phoneLabel.text = @"123456789";

    [self.view addSubview:cellView];

    cellView.tag = 1000;

    

//加手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    [cellView addGestureRecognizer:tap];

    

}

/*

-(void)rightAction:(UIBarButtonItem *)sender{

        //初始化

    UserinfoViewController *userVC = [[UserinfoViewController alloc]init];

        //赋值传值

    userVC.receiveTitle = self.titleField.text;

        //跳转

    [self.navigationController pushViewController:userVC animated:YES];

}

*/

//手势的回调方法

-(void)tapAction:(UITapGestureRecognizer *)sender{

    UserinfoViewController *userVC = [[UserinfoViewController alloc]init];

    [self.navigationController pushViewController:userVC animated:YES];

    

    userVC.delegate = self;


//    CellView *cell = (CellView *)[self.view viewWithTag:1000];

    CellView *cell = (CellView *)[sender view];       //和上句一样

        //将名字传到下个页面的文本框中

    userVC.receiveTitle = cell.nameLabel.text;

    

        //将所有信息都放入一个字典中

    NSDictionary *userInfoDic = [NSDictionary dictionaryWithObjectsAndKeys:cell.headImageView.image, @"headImage", cell.nameLabel.text,@"name",cell.phoneLabel.text,@"phone", nil];

        //将字典传过去

    userVC.receliveUserInfiDic = userInfoDic;

}



//实现用户详情界面的协议方法,用来接受详情界面传递过来的值,参数字典中的值就是我们所需要的值

-(void)passValueWithDic:(NSDictionary *)dictionary{

    //得到要展示内容的界面

    CellView *cellView = (CellView *)[self.view viewWithTag:1000];

    cellView.headImageView.image = [dictionary objectForKey:@"headImage"];

    cellView.nameLabel.text = [dictionary objectForKey:@"name"];

    cellView.phoneLabel.text = [dictionary objectForKey:@"phone"];

}







- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end




#import "UserinfoViewController.h"


@interface UserinfoViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@end


@implementation UserinfoViewController

#pragma mark -懒加载

//懒加载

-(UIImageView *)headImageView{

    if (!_headImageView) {

        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake((375-200)/2, 80, 200, 200)];

            //设置头像成圆的

        _headImageView.layer.cornerRadius =100;

        _headImageView.layer.masksToBounds = YES;

        _headImageView.layer.borderColor = [UIColor redColor].CGColor;

        _headImageView.layer.borderWidth = 5;

        

            //必须在初始化完成之后,再调用添加手势的方法

        [self forImageViewAddGestureWith:_headImageView];

            //打开用户界面   下面按钮会控制它,所以先注释掉

//        [_headImageView setUserInteractionEnabled:YES];

            //显示出来

        [self.view addSubview:_headImageView];

    }

    return _headImageView;

  }

-(UITextField *)nameTextField{

    if (!_nameTextField) {

        _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(150, 300, 100, 30)];

        _nameTextField.placeholder = @"姓名";

        [self.view addSubview:_nameTextField];

            //关闭textFiled的编辑功能

        _nameTextField.enabled = NO;

    }

    return _nameTextField;

}

-(UITextField *)phoneTextField{

    if (!_phoneTextField) {

        _phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(150, 350, 100, 30)];

        _phoneTextField.placeholder = @"电话";

        [self.view addSubview:_phoneTextField];

            //关闭编辑功能

        _phoneTextField.enabled = NO;

    }

    return _phoneTextField;

}




//给头像加轻拍手势,让它点击了就能在系统相册中改图片

    //初始化一个轻拍手势,添加到图片上

-(void)forImageViewAddGestureWith:(UIImageView *)imageView{

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction1:)];

    [imageView addGestureRecognizer:tap];

}





    

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.navigationItem.title = self.receiveTitle;

    

    self.headImageView.image = [self.receliveUserInfiDic objectForKey:@"headImage"];

    self.nameTextField.text = [self.receliveUserInfiDic objectForKey:@"name"];

    self.phoneTextField.text = [self.receliveUserInfiDic objectForKey:@"phone"];

    

    

//添加导航条右侧的按钮

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];

    

    self.navigationItem.rightBarButtonItem =rightButton;

//添加导航条左侧的按钮

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];

    

    self.navigationItem.leftBarButtonItem = leftButton;

    

    

}

//右按钮的方法: 点击一下,让文本框可以编辑(改变它的可编辑属性就ok)

-(void)rightButtonAction:(UIBarButtonItem *)sender{

    if ([sender.title isEqualToString:@"Edit"]) {

        sender.title = @"Done";

        self.nameTextField.enabled = YES;

        self.phoneTextField.enabled = YES;

        self.headImageView.userInteractionEnabled = YES;

    }else{

        sender.title = @"Edit";

        self.nameTextField.enabled = NO;

        self.phoneTextField.enabled = NO;

        self.headImageView.userInteractionEnabled = NO;

    }

}

//左侧按钮的方法:

-(void)leftButtonAction:(UIBarButtonItem *)sender{

    

    //在返回之前要将上个界面所要显示的值通过代理方法传递过去

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.headImageView.image,@"headImage",self.nameTextField.text,@"name",self.phoneTextField.text,@"phone", nil];

    

    [self.delegate passValueWithDic:dic];

    

    [self.navigationController popViewControllerAnimated:YES];

}









//图片手势的回调方法

-(void)tapAction1:(UITapGestureRecognizer *)sender{

    //调用系统相册

    UIImagePickerController *picker = [[UIImagePickerController alloc]init];

    //设置相册样式

    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        //设置图片可编辑

    picker.allowsEditing = YES;

    //通过模态的方式推出系统相册 //注意:要打开用户交互

    [self presentViewController:picker animated:YES completion:nil];

    //设置代理,只有通过代理方法才可以得到图片(2个代理)

    picker.delegate = self;

}




#pragma mark -实现imagePickerVC 系统相册的代理方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //取得所选取的图片

    UIImage *selectImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    self.headImageView.image = selectImage;

        //模态的返回

    [picker dismissViewControllerAnimated:YES completion:nil];

}














- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





0 0
原创粉丝点击