#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;
}
#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