#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