#import "AppDelegate.h"
#import "UserListTableViewController.h"
#import "DetailViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UserListTableViewController *userList = [UserListTableViewController new];
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:userList];
self.window.rootViewController = navC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import "UserListTableViewController.h"
#import "UserInfoModel.h"
#import "UserListCell.h"
#import "DetailViewController.h"
#import "AddUserViewController.h"
@interface UserListTableViewController ()
@property(nonatomic,retain)NSMutableDictionary *allDataDic; //存放所有分区联系人
@property(nonatomic,retain)NSMutableDictionary *allModeDic; //存放所有转化为model对象的联系人
@property(nonatomic,retain)NSMutableArray *allKeysMutableArray; //存放排序之后所有分区标题
@property(nonatomic,retain)UISearchBar *searchBar;//搜索框
@end
@implementation UserListTableViewController
//懒加载
-(NSMutableDictionary *)allDataDic{
if (!_allDataDic) {
_allDataDic = [NSMutableDictionary new];
}
return _allDataDic;
}
-(NSMutableDictionary *)allModeDic{
if (!_allModeDic) {
_allModeDic = [NSMutableDictionary new];
}
return _allModeDic;
}
//为字典所有的key值排序(冒泡排序)
-(void)sortAllKeys:(NSArray *)array{
//先将不可变数组转换为可变数组,只有可变数组才能交换内部元素的顺序
_allKeysMutableArray = [[NSMutableArray alloc]initWithArray:array];
for (int i = 0; i<_allKeysMutableArray.count-1; i++) {
for (int j = 0;j<_allKeysMutableArray.count-1-i; j++) {
if ([_allKeysMutableArray[j] compare:_allKeysMutableArray[j+1]]>0) {
[_allKeysMutableArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
}
//制造假数据
-(void)creatData{
NSDictionary *person_1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"A-HanOBa",@"name",@"18",@"age",@"110",@"phone",@"Man",@"gender",nil];
NSArray *tempArray = [NSArray arrayWithObjects:person_0,person_1,person_2,person_3,person_4,person_5,person_6,person_7,person_8,person_9,person_10,person_11,person_12,person_13,person_14,person_15,person_16,person_17,person_18,person_19,person_20,person_21,person_22,person_23,person_24,person_25,person_26,person_27,person_28,person_29,person_30,person_31,person_32, nil];
//将所有联系人按分区加载
for (int i = 0; i<tempArray.count; i++) {
//先从临时数组中将字典取出
NSDictionary *userInfoDic = [tempArray objectAtIndex:i];
//从字典中取出联系人姓名
NSString *userName = [userInfoDic objectForKey:@"name"];
//取出姓名的大写首字母
NSString *upFirstStr = [self transformCharacter:userName];
//先从大字典中按照key值(姓名首字母)取值,如果可以取出,说明该分区所对应的可变数组已经创建,直接将联系人添加进去即可。如果取出的值为空对象,说明该分区对应的可变数组未创建,先创建可变数组,在添加联系人
NSMutableArray *mutableArray = [self.allDataDic objectForKey:upFirstStr];
if (mutableArray) {//可变数组存在,直接添加
[mutableArray addObject:userInfoDic];
}else{//可变数组不存在,先创建,在添加
mutableArray = [[NSMutableArray alloc]initWithObjects:userInfoDic, nil];
[self.allDataDic setObject:mutableArray forKey:upFirstStr];
}
}
//为字典所有的key排序
[self sortAllKeys:self.allDataDic.allKeys];
//将所有的小字典转换为model并且存储
[self dicTransFormTOModelWithDic:self.allDataDic];
}
//将所有联系人字典转换为model
-(void)dicTransFormTOModelWithDic:(NSDictionary *)dic{
//首先将allDataDic中的可变数组取出,然后在取出可变数组中的联系人字典,将此字典转换为Model
for (NSString *key in _allKeysMutableArray) {
NSArray *smallArray = [self.allDataDic objectForKey:key];
//声明一个可变数组,用来存储所有的model类,也就是每个分区下的联系人。
NSMutableArray *sectionMArray = [[NSMutableArray alloc]init];
for (int i = 0; i<smallArray.count; i++) {
NSDictionary *userInfoDictionary = [smallArray objectAtIndex:i];
//将字典转换为model,并存储到allmoDelDictionary中
UserInfoModel *userInfoModel = [[UserInfoModel alloc]init];
[userInfoModel setValuesForKeysWithDictionary:userInfoDictionary];
[sectionMArray addObject:userInfoModel];
}
//在内层for循环的外部,将可变数组添加进allModelDic
[self.allModeDic setObject:sectionMArray forKey:key];
}
}
//汉子转拼音之后,截取首字母,并大写
-(NSString *)transformCharacter:(NSString *)sourceStr{
//先将原字符串转换为可变字符串
NSMutableString *ms = [NSMutableString stringWithString:sourceStr];
if (ms.length) {
//将汉子转化为拼音
CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformToLatin, NO);
//将拼音的声调去掉
CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO);
//将字符串所有的字母大写
NSString *upStr = [ms uppercaseString];
//截取首字母
NSString *firstStr = [upStr substringToIndex:1];
return firstStr;
}
return @"*";
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"通讯录";
self.tableView.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//创建数据
[self creatData];
//注册单元格
[self.tableView registerClass:[UserListCell class] forCellReuseIdentifier:@"CELL"];
//隐藏掉系统的分割线
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
self.tableView.showsVerticalScrollIndicator = NO;
//为导航条添加一个左边按钮
self.navigationItem.leftBarButtonItem = self.editButtonItem;
//为导航条添加右按钮
UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUserAction:)];
self.navigationItem.rightBarButtonItem = rightBtnItem;
//搜索框的基本属性
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 5, 50, 30)];
[self.tableView addSubview:searchBar];
searchBar.placeholder = @"搜索通讯录";
[self.navigationController.navigationBar.topItem setTitleView:searchBar];
searchBar.showsBookmarkButton =YES;
}
//右按钮实现方法(实现添加联系人功能)
-(void)addUserAction:(UIBarButtonItem *)sender{
AddUserViewController *addUserVC = [[AddUserViewController alloc]init];
//实现block,model就是传递过来的值
addUserVC.block = ^(UserInfoModel *model){
NSLog(@"%@",model.name);//检测值传过来了没有?
//因为要确定该联系人应该添加到那个分区下,所以需要知道姓名的首字母
NSString *firstStr = [self transformCharacter:model.name];
NSMutableArray *mArray = [self.allModeDic objectForKey:firstStr];
if (mArray) { //说明该分区存在
[mArray addObject:model];
}else{ //说明该分区不存在,需要先创建,再添加
mArray = [[NSMutableArray alloc]initWithObjects:model, nil];
[self.allModeDic setObject:mArray forKey:firstStr];
//所有的key排序
[self sortAllKeys:self.allModeDic.allKeys];
}
//刷新
[self.tableView reloadData];
};
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:addUserVC];
[self presentViewController:navC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.allModeDic.count;
}
//每个分区下有多少行表格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//先根据section取出key值
NSString *key = [_allKeysMutableArray objectAtIndex:section];
//根据key值取出可变数组
NSArray *array = [self.allModeDic objectForKey:key];
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UserListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
//1.根据section取出小数组
NSString *key = [_allKeysMutableArray objectAtIndex:indexPath.section];
NSArray *array = [self.allModeDic objectForKey:key];
//2.根据row从小数组中取出model
UserInfoModel *model = [array objectAtIndex:indexPath.row];
//3.为控件赋值
cell.nameLabel.text = model.name;
cell.phoneLabel.text = model.phone;
cell.headImageView.image = [UIImage imageNamed:@"Two.png"];
return cell;
}
//返回每个分区标题的代理方法:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_allKeysMutableArray objectAtIndex:section];
}
//返回行高的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
//索引条的方法
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _allKeysMutableArray;
}
//点击单元格所触发的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
//取值
NSString *key = [_allKeysMutableArray objectAtIndex:indexPath.section];
NSArray *array = [self.allModeDic objectForKey:key];
UserInfoModel *model = [array objectAtIndex:indexPath.row];
//传值
detailVC.title = model.name;
detailVC.nameLabel.text = model.name;
detailVC.ageLabel.text = model.age;
detailVC.genderLabel.text = model.gender;
detailVC.phoneLabel.text = model.phone;
detailVC.receiveModel = model;
}
#pragma mark -删除操作的方法
//当删除操作结束时执行的代理方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据
NSString *key = [_allKeysMutableArray objectAtIndex:indexPath.section];
NSMutableArray *array = [self.allModeDic objectForKey:key];
[array removeObjectAtIndex:indexPath.row];
//删除单元格,当可变数组的数量为0的时候,说明该分区下没有联系人了,同时删除分区.如果不为0,说明该数组下还有联系人,值删除所在的单元格即可
if (array.count) { //说明该分区下还有联系人,只删除该联系人
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}else{ //说明该分区下已经没有联系人,将分区也同时删掉
//从大字典中将该可变数组删除
[self.allModeDic removeObjectForKey:key];
//将排好序的键的数组(_allKeysMutabelArray)中将该分区标题删除
[self.allKeysMutableArray removeObject:key];
//删除分区
[tableView deleteSections:[NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation:UITableViewRowAnimationBottom];
}
}
}
#pragma mark -移动单元格的方法
//使得所有的单元格移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//移动完成执行的代理方法,注意sourceIndexPath!
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSString *key = [_allKeysMutableArray objectAtIndex:sourceIndexPath.section];
NSMutableArray *array = [self.allModeDic objectForKey:key];
UserInfoModel *model = [array objectAtIndex:sourceIndexPath.row];
//删除数组中原位置的联系人
[array removeObjectAtIndex:sourceIndexPath.row];
//将model插入到新的位置
[array insertObject:model atIndex:sourceIndexPath.row];
}
//检测移动过程,让不同分区之间不可以移动单元格
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
if (sourceIndexPath.section ==proposedDestinationIndexPath.section) {//这句判断说明在同一分区,可以移动
return proposedDestinationIndexPath;
}else{
//说明跨区移动,所以不能移动
return sourceIndexPath;
}
}
#import <UIKit/UIKit.h>
#import "UserInfoModel.h"
@interface DetailViewController : UIViewController
@property(nonatomic,retain)NSString *titerName; //名字
@property(nonatomic,retain)UIImageView *headImageView; //显示头像
@property(nonatomic,retain)UILabel *nameLabel; //姓名
@property(nonatomic,retain)UILabel *phoneLabel; //电话
@property(nonatomic,retain)UILabel *genderLabel; //性别
@property(nonatomic,retain)UILabel *ageLabel; //年龄
//接收上个界面传递过来的联系人信息
@property(nonatomic,retain)UserInfoModel *receiveModel;
@end
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
-(UIImageView *)headImageView{
if (!_headImageView) {
_headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 200, 200)];
_headImageView.layer.cornerRadius = 100;
_headImageView.layer.masksToBounds = YES;
_headImageView.layer.borderColor = [UIColor redColor].CGColor;
_headImageView.layer.borderWidth = 5;
[self.view addSubview:_headImageView];
}
return _headImageView;
}
-(UILabel *)nameLabel{
if (!_nameLabel) {
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 300, 150, 30)];
_nameLabel.layer.cornerRadius =5;
_nameLabel.layer.masksToBounds = YES;
_nameLabel.layer.borderColor = [UIColor redColor].CGColor;
_nameLabel.layer.borderWidth = 3;
_nameLabel.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_nameLabel];
}
return _nameLabel;
}
-(UILabel *)phoneLabel{
if (!_phoneLabel) {
_phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 350, 150, 30)];
_phoneLabel.layer.cornerRadius =5;
_phoneLabel.layer.masksToBounds = YES;
_phoneLabel.layer.borderColor = [UIColor redColor].CGColor;
_phoneLabel.layer.borderWidth = 3;
_phoneLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_phoneLabel];
}
return _phoneLabel;
}
-(UILabel *)genderLabel{
if (!_genderLabel) {
_genderLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 400, 150, 30)];
_genderLabel.layer.cornerRadius =5;
_genderLabel.layer.masksToBounds = YES;
_genderLabel.layer.borderColor = [UIColor redColor].CGColor;
_genderLabel.layer.borderWidth = 3;
_genderLabel.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_genderLabel];
}
return _genderLabel;
}
-(UILabel *)ageLabel{
if (!_ageLabel) {
_ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 450, 150, 30)];
_ageLabel.layer.cornerRadius =5;
_ageLabel.layer.masksToBounds = YES;
_ageLabel.layer.borderColor = [UIColor redColor].CGColor;
_ageLabel.layer.borderWidth = 3;
_ageLabel.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_ageLabel];
}
return _ageLabel;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.navigationItem.title = self.titerName;
//为联系人详情赋值
self.headImageView.image = [UIImage imageNamed:@"Two.png"];
//创建姓名,性别,电话,年龄的标签
NSArray *array = [NSArray arrayWithObjects:@"姓名:",@"电话:",@"性别:",@"年龄:" ,nil];
for (int i = 0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(70, 300+i*50, 80, 30)];
label.text = array[i];
label.textColor = [UIColor blackColor];
[self.view addSubview:label];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <Foundation/Foundation.h>
@interface UserInfoModel : NSObject
@property(nonatomic,retain)NSString *name; //姓名
@property(nonatomic,retain)NSString *gender; //性别
@property(nonatomic,retain)NSString *age; //年龄
@property(nonatomic,retain)NSString *phone; //电话
@end
#import "UserInfoModel.h"
@implementation UserInfoModel
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"如果键值不对应的话,打印出来---%@",key);
}
@end
#import <UIKit/UIKit.h>
@interface UserListCell : UITableViewCell
@property(nonatomic,retain)UIImageView *headImageView; //显示头像的框
@property(nonatomic,retain)UILabel *nameLabel; //显示姓名
@property(nonatomic,retain)UILabel *phoneLabel; //显示电话号码
@end
#import "UserListCell.h"
@implementation UserListCell
//分割线
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//加载分割线
UILabel *separatorLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100-1, [UIScreen mainScreen].bounds.size.width, 1)];
separatorLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:separatorLabel];
}
return self;
}
//懒加载 头像
-(UIImageView *)headImageView{
if (!_headImageView) {
_headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];
_headImageView.layer.masksToBounds = YES;
_headImageView.layer.cornerRadius = 40;
_headImageView.layer.borderColor = [UIColor redColor].CGColor;
_headImageView.layer.borderWidth = 3;
[self.contentView addSubview:_headImageView];
}
return _headImageView;
}
//名字
-(UILabel *)nameLabel{
if (!_nameLabel) {
_nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 200, 30)];
[self.contentView addSubview:_nameLabel];
}
return _nameLabel;
}
//电话号码
-(UILabel *)phoneLabel{
if (!_phoneLabel) {
_phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 60, 200, 30)];
[self.contentView addSubview:_phoneLabel];
}
return _phoneLabel;
}
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import <UIKit/UIKit.h>
#import "UserInfoModel.h"
typedef void(^passValueBlock)(UserInfoModel *value);
@interface AddUserViewController : UIViewController
//声明一个Block属性
@property(nonatomic,copy)passValueBlock block;
@end
#import "AddUserViewController.h"
@interface AddUserViewController ()
@property(nonatomic,retain)UIImageView *headImageView; //显示头像
@property(nonatomic,retain)UITextField *nameTextField; //姓名
@property(nonatomic,retain)UITextField *phoneTextField; //电话
@property(nonatomic,retain)UITextField *genderTextField; //性别
@property(nonatomic,retain)UITextField *ageTextField; //年龄
@end
@implementation AddUserViewController
//懒加载
-(UIImageView *)headImageView{
if (!_headImageView) {
_headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 200, 200)];
_headImageView.layer.cornerRadius = 100;
_headImageView.layer.masksToBounds = YES;
_headImageView.layer.borderColor = [UIColor redColor].CGColor;
_headImageView.layer.borderWidth = 5;
[self.view addSubview:_headImageView];
}
return _headImageView;
}
-(UITextField *)nameTextField{
if (!_nameTextField) {
_nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 300, 150, 30)];
_nameTextField.layer.cornerRadius =5;
_nameTextField.layer.masksToBounds = YES;
_nameTextField.layer.borderColor = [UIColor redColor].CGColor;
_nameTextField.layer.borderWidth = 3;
_nameTextField.placeholder = @"Name";
_nameTextField.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_nameTextField];
}
return _nameTextField;
}
-(UITextField *)phoneTextField{
if (!_phoneTextField) {
_phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 350, 150, 30)];
_phoneTextField.layer.cornerRadius =5;
_phoneTextField.layer.masksToBounds = YES;
_phoneTextField.layer.borderColor = [UIColor redColor].CGColor;
_phoneTextField.layer.borderWidth = 3;
_phoneTextField.placeholder = @"Phone";
_phoneTextField.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_phoneTextField];
}
return _phoneTextField;
}
-(UITextField *)genderTextField{
if (!_genderTextField) {
_genderTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 400, 150, 30)];
_genderTextField.layer.cornerRadius =5;
_genderTextField.layer.masksToBounds = YES;
_genderTextField.layer.borderColor = [UIColor redColor].CGColor;
_genderTextField.layer.borderWidth = 3;
_genderTextField.placeholder = @"Gender";
_genderTextField.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_genderTextField];
}
return _genderTextField;
}
-(UITextField *)ageTextField{
if (!_ageTextField) {
_ageTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 450, 150, 30)];
_ageTextField.layer.cornerRadius =5;
_ageTextField.layer.masksToBounds = YES;
_ageTextField.layer.borderColor = [UIColor redColor].CGColor;
_ageTextField.layer.borderWidth = 3;
_ageTextField.placeholder = @"Age";
_ageTextField.textAlignment =NSTextAlignmentCenter;
[self.view addSubview:_ageTextField];
}
return _ageTextField;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"添加联系人";
self.view.backgroundColor = [UIColor yellowColor];
self.headImageView.image = [UIImage imageNamed:@"Two.png"];
//添加界面
[self headImageView];
[self nameTextField];
[self phoneTextField];
[self ageTextField];
[self genderTextField];
//创建姓名,性别,电话,年龄的标签
NSArray *array = [NSArray arrayWithObjects:@"姓名:",@"电话:",@"性别:",@"年龄:" ,nil];
for (int i = 0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(70, 300+i*50, 80, 30)];
label.text = array[i];
label.textColor = [UIColor blackColor];
[self.view addSubview:label];
}
//添加左侧按钮(返回)
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
//添加右侧按钮(完成)
UIBarButtonItem *righrBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishAction:)];
self.navigationItem.rightBarButtonItem = righrBtn;
}
//左侧按钮(返回)的实现方法
-(void)backAction:(UIBarButtonItem *)sender{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
//右侧按钮(完成)的实现方法
-(void)finishAction:(UIBarButtonItem *)sender{
NSString *message = nil;
if ((!self.nameTextField.text) ||[self.nameTextField.text isEqualToString:@""]) {
message = @"亲,您还没有输入用户名喔,么么哒";
}else if ((!self.phoneTextField.text) ||[self.phoneTextField.text isEqualToString:@""]){
message = @"亲,您还没有输入电话喔,么么哒";
}else if((!self.ageTextField.text) ||[self.ageTextField.text isEqualToString:@""]){
message = @"亲,您还没有输入年龄喔,么么哒";
}else if((!self.genderTextField.text) ||[self.genderTextField.text isEqualToString:@""]){
message = @"亲,您还没有输入性别喔,么么哒";
}else{
//初始化model类
UserInfoModel *model = [[UserInfoModel alloc]init];
model.name = self.nameTextField.text;
model.age = self.ageTextField.text;
model.gender = self.genderTextField.text;
model.phone = self.phoneTextField.text;
//block传值
self.block(model);
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
if (message) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"友情提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
[alertController addAction:sureAction];
//弹出
[self presentViewController:alertController animated: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