2、当前一个界面要展示的数据,依赖于后一个界面时,此时使用模态的形式推出视图,模态:是一种新的视图推出方式
通讯录实战所需素材:
http://pan.baidu.com/s/1o6nFol8
———————————————————————————————
ContactListTableViewController.m
#import "ContactListTableViewController.h"
#import "AddContactViewController.h"
#import "DetailContactController.h"
#import "CustomCell.h"
#import "AddContactViewController
#import "DetailContactController.h"
#import "CustomCell.h"
#import "Contacter.h"
- (void)viewDidLoad {
[ super viewDidLoad];
[ self configureNavigatinControllerBarContent ];
}
//配置导航条的内容
- (void)configureNavigatinControllerBarContent{
//设置title
self.navigationItem.title = @"通讯录";
//设置左边添加联系人按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"add_contact"]style:(UIBarButtonItemStylePlain ) target:self action:@selector(handleAdd: )];
self.navigationItem.leftBarButtonItem = leftItem;
//配置导航条的背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
//配置导航条内容的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//修改title的字体颜色
NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.navigationController.navigationBar.titleTextAttributes = dic;
//右侧导航条为编辑按钮
}
//配置导航条的内容
- (void)configureNavigatinContro
}
#pragma mark 添加联系人按钮的方法
- (void)handleAdd: (UIBarButtonItem *)leftItem{
NSLog(@"添加联系人");
//当前一个界面要展示的数据,依赖于后一个界面时,此时使用模态的形式推出视图,模态:是一种新的视图推出方式
//先创建要推出的试图控制器对象
AddContactViewController *addVC =[[AddContactViewController alloc]init];
//给模态出来的视图控制添加导航控制器
UINavigationController *navogationVC = [[UINavigationController alloc]initWithRootViewController :addVC];
//配置模态出现的样式
navogationVC. modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
//UIModalTransitionStyleCoverVertical = 0,
//UIModalTransitionStyleFlipHorizontal,
//UIModalTransitionStyleCrossDissolve,
#pragma mark 添加联系人按钮的方法
- (void)handleAdd: (UIBarButtonItem *)leftItem{
}
- (void)didReceiveMemoryWarning {
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:( UITableView *)tableView {
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *idntifer = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier :idntifer];
if (cell == nil) {
cell = [[ CustomCell alloc]initWithStyle:(UITableViewCellStyleValue1 ) reuseIdentifier:idntifer];
}
//准备一个字典,使用给Model对象赋值
NSDictionary *dic = @{@"name":@"小韩哥",@"gender'":@"女",@"age":@"18",@"says":@"我爱编程",@"phone":@"7788521",@"imageName":@"1.png"};
//给model类赋值Contecter
Contacter *contacter = [[Contacter alloc]init];
contacter. name = dic[@"name"];
//使用KVC赋值
[contacter setValuesForKeysWithDictionary :dic];
cell. photoView.image = contacter.image;
cell. nameLabel.text = contacter.name;
cell. phoneLabel.text = contacter.phone;
//给model类赋值Contecter
// NSLog(@"%@- %@-%@-%@-%@-%@",contacter.name,contacter.age,contacter.gender,contacter.phone,contacter.says,contacter.image);//打印看是否取到Model数据的value值
}
#pragma mark 点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
//通过代理返回cell的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
——————————————————————————————————
添加联系人:
AddContactViewController.m
#import "AddContactViewController.h"
#import "CustomView.h"
@interface AddContactViewController ()<</span>UIActionSheetDelegate,UIImagePickerControllerDelegate ,UINavigationControllerDelegate >
#import "CustomView.h"
@interface AddContactViewController
@property(nonatomic,retain)CustomView *aview;
@end
@implementation AddContactViewController
- ( void)dealloc{
self.aview = nil;
[ super dealloc];
}
- (void)loadView{
self.aview = [[CustomView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.aview;
[ self.aview release];
@implementation AddContactViewController
- (
}
- (void)loadView{
}
- (void)viewDidLoad {
[ super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
[ self configureNavigationBarContent ];
//调用手势方法
[ self addTapGesture];
}
//给aImageView 视图添加轻拍手势
- (void)addTapGesture{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap : )];
[ self.aview.aImageView addGestureRecognizer:tap];
}
- (void)addTapGesture{
}
//实现轻拍手势的方法
- (void)handleTap : (UITapGestureRecognizer *)tap{
//添加ActionSheet控件 提示选项框
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从手机中选择", nil];
//在当前界面显示actionSheet对象
[actionSheet showInView:self.view];
[actionSheet release];
//实现轻拍手势的方法
- (void)handleTap : (UITapGestureRecognizer *)tap{
//添加ActionSheet控件 提示选项框
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickeonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
//拍照
NSLog(@"拍照");
[ self pickerPictureFromCamera];
break;
case 1:
//从相册中读取照片
NSLog(@"从相册中读取照片");
[ self pickerPictureFormPhotoAlbum ];
break;
default:
break;
}
}
//拍照
- (void)pickerPictureFromCamera{
//判断前摄像头是否可以使用
BOOL isCameera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront ];
// UIImagePickerControllerCameraDeviceFront 前摄像头
// UIImagePickerControllerCameraDeviceRear //后摄像头
if (!isCameera) {
NSLog(@"没有摄像头可以使用");
return;
}
//初始化图片选择控制器对象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置图片选择器选取图片的样式
imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
//设置取出来的图片是否允许编辑
imagePicker. allowsEditing = YES;
}
- (void)pickerPictureFromCamera{
BOOL isCameera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerC
//
//
//从相册中取出相片
- (void)pickerPictureFormPhotoAlbum{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置图片格式
imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
//设置允许编辑
imagePicker. allowsEditing = YES;
//设置代理
imagePicker. delegate = self;
[ self presentViewController:imagePicker animated:YES completion:nil];
- (void)pickerPictureFormPhotoAl
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info{
//从字典中取出编辑的key值,对应的照片
self.aview.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage ];
//自己推出来的自己收回去
[ self dismissViewControllerAnimated :YES completion:nil];
}
//配置导航条
- (void)configureNavigationBarContent{
self.navigationItem.title = @"添加联系人";
//配置导航条背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
//配置内容页的渲染颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//配置字体颜色
NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.navigationController.navigationBar.titleTextAttributes = dic;
//左侧取消按钮
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"clsose"] style:(UIBarButtonItemStylePlain ) target:self action:@selector(hanlBack : )];
self.navigationItem.leftBarButtonItem = leftItem;
//释放
[leftItem release];
//右侧保存按钮
- (void)configureNavigationBarCo
}
#pragma mark 取消按钮的实现
- (void)hanlBack : ( UIBarButtonItem *)item{
//返回上一级界面
[ self dismissViewControllerAnimated :YES completion:nil];
}
#pragma mark 保存按钮的实现
- (void)handleSave : (UIBarButtonItem *)item{
//保存数据操作
//保存数据后同样返回上一个界面
[ self dismissViewControllerAnimated :YES completion:nil];
#pragma mark 取消按钮的实现
- (void)hanlBack :
}
- (void)handleSave : (UIBarButtonItem *)item{
}
添加联系人效果:
——————————————————————————————
详情界面:与上面添加联系人界面大同小异
DetailContactController.m
#import "DetailContactController.h"
#import "CustomView.h"
@interface DetailContactController ()<</span>UIActionSheetDelegate,UINavigationControllerDelegate ,UIImagePickerControllerDelegate >
@property(nonatomic,retain)CustomView *aView;
#import "CustomView.h"
@interface DetailContactController ()<</span>UIActionSheetDelegate,UINavigationControllerDe
@property(nonatomic,retain)CustomView *aView;
@end
@implementation DetailContactController
- (void)dealloc{
self.aView = nil;
[ super dealloc];
- (void)dealloc{
}
- (void)loadView{
self.aView = [[CustomView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.aView;
}
//控制self.aView 的交互事件方法
- (void)closeUserInteractionByBool : ( BOOL)isTure{
//根据传进来的参数决定self.aView交互是否打开
self.aView.userInteractionEnabled = isTure;
- (void)closeUserInteractionByBo
}
- (void)viewDidLoad {
[ super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//配置导航条的内容
[ self configureNavigationBarContent ];
//进来详情页面先不能编辑
[ self closeUserInteractionByBool :NO];
//调用添加手势的方法
//配置导航条的内容
}
//添加aImageView添加轻拍手势
- (void)addTapGesture{
- (void)addTapGesture{
}
//实现轻拍手势的方法
- (void)handleTap :
}
- (
}
- (void)configureNavigationBarCo
}
//拍照
- (void)pickerPictureFromCamera{
//判断前摄像头是否可以使用
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront ];
if (!isCamera) {
return;
}
//初始化图片是选择器对象
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置图片选择器选取图片的样式
imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
//设置取出来的图片是否允许编辑
imagePicker. allowsEditing = YES;
//设置代理
imagePicker. delegate = self;
//把手机相机推出来
[ self presentViewController:imagePicker animated:YES completion:nil];
//拍照
- (void)pickerPictureFromCamera{
}
//从相册中取相片
- (void)pickerPictureFromPhotoAlbum{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//设置图片格式
imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary ;
//设置是否允许编辑
imagePicker. allowsEditing = YES;
//设置代理
imagePicker. delegate = self;
[ self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:( NSDictionary *)info{
//从字典中取出编辑的key值,对应的照片
self.aView.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage ];
//自己推出来自己收回去
//从相册中取相片
- (void)pickerPictureFromPhotoAl
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWit
}
#pragma mark 重写编辑按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
}
//实现back的方法
- (void)handleBack: (UIBarButtonItem *)back{
- (void)handleBack: (UIBarButtonItem *)back{
}
详情界面编辑效果:
————————————————————————————————————
CustomCell 和CustomView 自定义cell和自定义视图布局不做详细介绍!
2、懒加载方法:
CustomCell.h
@property(nonatomic,retain)UIImageView *photoView;
@property(nonatomic,retain)UILabel *nameLabel;
@property(nonatomic,retain)UILabel *nameLabel;
@property(nonatomic,retain)UILabel *phoneLabel;
CustomCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//布局操作写在这里
[ self.contentView addSubview:self.photoView];
[ self.contentView addSubview:self.nameLabel];
}
//懒加载创建cell上的自定义控件
- (UIImageView *)photoView{
if (_photoView == nil) {
self.photoView = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 80, 80)]autorelease];
self.photoView.layer.cornerRadius = 40.0;
self.photoView.layer.masksToBounds = YES;
}
- (UILabel *)nameLabel{
if (_nameLabel == nil) {
self.nameLabel = [[[UILabel alloc]initWithFrame:CGRectMake(100, 18, 100, 25)]autorelease];
self.nameLabel.backgroundColor = [UIColor brownColor];
self.nameLabel.layer.cornerRadius = 7;
self.nameLabel.layer.masksToBounds = YES;
}
return [[_nameLabel retain]autorelease];
}
- (UILabel *)phoneLabel{
if (_phoneLabel == nil) {
self.phoneLabel = [[[UILabel alloc]initWithFrame:CGRectMake(100, 48, 160, 25)]autorelease];
self.phoneLabel.backgroundColor = [UIColor brownColor];
self.phoneLabel.layer.cornerRadius = 7;
self.phoneLabel.layer.masksToBounds = YES;
}
return [[_phoneLabel retain]autorelease];
}
- (UILabel *)phoneLabel{
}
——————————————————————————————————
欢迎学习本文,未经许可,禁止转载!