iOS开发UI篇—实现一个私人通讯录小应用(二)
生活随笔
收集整理的這篇文章主要介紹了
iOS开发UI篇—实现一个私人通讯录小应用(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、實現功能說明
(1)點擊注銷按鈕,彈出一個對話框,點擊確定后移除當前棧頂的控制器,返回開始界面,點擊取消,不做任何操作。
注意:注銷按鈕的單擊事件已經進行了連線。實現-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex需要遵守UIActionSheetDelegate協議。
1 //注銷按鈕 2 - (IBAction)logoutBtn:(id)sender { 3 4 UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"確定要注銷?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles: nil]; 5 6 [sheet showInView:self.view]; 7 } 8 9 #pragma mark-代理方法 10 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 11 { 12 if (buttonIndex!=0)return; 13 //移除棧頂的控制器 14 [self.navigationController popViewControllerAnimated:YES]; 15 }2)當兩個文本框的狀態發生改變時,通知添加按鈕變為可用狀態。
知識點:通知(注冊監聽)
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 //1.獲得通知中心 6 NSNotificationCenter *center=[NSNotificationCenter defaultCenter]; 7 //2.注冊監聽 8 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameFeild]; 9 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneFeild]; 10 } 11 12 13 14 //當文本框內容改變的時候,通知self調用該方法 15 -(void)textChange 16 { 17 //判斷,如果兩個文本框的內容都改變(有值)的時候,添加按鈕變成可交互的 18 self.addBtn.enabled=(self.nameFeild.text.length>0&&self.phoneFeild.text.length>0); 19 NSLog(@"通知調用的事件"); 20 } 21 22 //臨終遺言 23 -(void)dealloc 24 { 25 [[NSNotificationCenter defaultCenter] removeObserver:self]; 26 }(3)數據的逆傳(使用代理)
YYContatcsViewController.m文件
1 // 2 // YYContatcsViewController.m 3 // 01-私人通訊錄(登錄頁面搭建) 4 // 5 // Created by apple on 14-6-8. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYContatcsViewController.h" 10 #import "YYAddViewController.h" 11 #import "YYInfoModel.h" 12 13 //遵守協議 14 @interface YYContatcsViewController ()<UIActionSheetDelegate,YYAddViewControllerDelegate> 15 @property (strong, nonatomic) IBOutlet UITableView *tableview; 16 17 //數組,用來保存用戶添加的數據 18 @property(nonatomic,strong)NSMutableArray *array; 19 20 - (IBAction)logoutBtn:(id)sender; 21 22 @end 23 24 @implementation YYContatcsViewController 25 26 27 - (void)viewDidLoad 28 { 29 [super viewDidLoad]; 30 } 31 32 //注銷按鈕 33 - (IBAction)logoutBtn:(id)sender { 34 35 UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"確定要注銷?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles: nil]; 36 37 [sheet showInView:self.view]; 38 } 39 40 #pragma mark-代理方法 41 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 42 { 43 if (buttonIndex!=0)return; 44 //移除棧頂的控制器 45 [self.navigationController popViewControllerAnimated:YES]; 46 } 47 48 49 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 50 { 51 UIViewController *c=segue.destinationViewController; 52 YYAddViewController *addc=(YYAddViewController *)c; 53 addc.delegate=self; 54 55 } 56 57 #pragma mark-YYAddViewControllerDelegate 58 -(void)addViewControllerDidAddBtn:(YYAddViewController *)addViewController contatc:(YYInfoModel *)contatc 59 { 60 //1.把數組保存到數組中 61 // [self.Info addObject:contatc]; 62 // //2.刷新表格 63 // NSLog(@"%@",contatc); 64 // NSLog(@"%@",self.Info); 65 // [self.tableview reloadData]; 66 NSLog(@"%@,%@",contatc.name,contatc.phone); 67 [self.array addObject:contatc]; 68 [self.tableview reloadData]; 69 70 } 71 72 -(NSMutableArray *)array 73 { 74 if (_array==Nil) { 75 _array=[NSMutableArray array]; 76 } 77 return _array; 78 } 79 #pragma mark-tableview的數據源 80 //一共有多少行 81 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 82 { 83 // return self.Info.count; 84 return self.array.count; 85 } 86 //每組每行的cell 87 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 88 { 89 static NSString *identifier=@"info"; 90 //先去緩存中取。如果緩存中沒有,那么就到storyboard中去查找 91 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 92 //在storyboard中設置cell的標識符為info 93 94 //設置cell的數據 95 // 96 // UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Nil]; 97 98 YYInfoModel *info=self.array[indexPath.row]; 99 cell.textLabel.text=info.name; 100 cell.detailTextLabel.text=info.phone; 101 102 103 //返回cell 104 return cell; 105 } 106 @endYYAddViewController.h文件
1 // 2 // YYAddViewController.h 3 // 01-私人通訊錄(登錄頁面搭建) 4 // 5 // Created by 孔醫己 on 14-6-8. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 @class YYAddViewController, YYInfoModel; 11 //自定義一個協議,讓上一個控制器(YYContatcsViewController)成為當前控制器的代理 12 13 //@protocol YYAddViewControllerDelegate <NSObject> 14 // 15 ////協議方法 16 //-(void)addViewControllerDidAddBtn:(YYAddViewController *)addViewController contatc:(YYInfoModel *)contatc; 17 18 @protocol YYAddViewControllerDelegate <NSObject> 19 20 //- (void)editViewControllerDidAddBtn:(NJEditViewController *)editViewController name:(NSString *)name number:(NSString *)phoneNumber; 21 22 - (void)addViewControllerDidAddBtn:(YYAddViewController *)editViewController contatc:(YYInfoModel *)contatc; 23 24 @end 25 26 @interface YYAddViewController : UIViewController 27 28 //新增一個代理屬性 29 @property(nonatomic,strong)id<YYAddViewControllerDelegate> delegate; 30 31 @endYYAddViewController.m文件
1 // 2 // YYAddViewController.m 3 // 01-私人通訊錄(登錄頁面搭建) 4 // 5 // Created by 孔醫己 on 14-6-8. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYAddViewController.h" 10 #import "YYInfoModel.h" 11 12 @interface YYAddViewController () 13 //姓名輸入框 14 @property (weak, nonatomic) IBOutlet UITextField *nameFeild; 15 //電話號碼輸入框 16 @property (weak, nonatomic) IBOutlet UITextField *phoneFeild; 17 //添加按鈕 18 @property (weak, nonatomic) IBOutlet UIButton *addBtn; 19 20 //添加按鈕的點擊事件 21 - (IBAction)addBtnOnclick:(id)sender; 22 23 @end 24 25 @implementation YYAddViewController 26 27 28 #pragma mark- 監聽是否添加 29 - (void)viewDidLoad 30 { 31 [super viewDidLoad]; 32 33 //1.獲得通知中心 34 NSNotificationCenter *center=[NSNotificationCenter defaultCenter]; 35 //2.注冊監聽 36 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameFeild]; 37 [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneFeild]; 38 } 39 40 41 42 //當文本框內容改變的時候,通知self調用該方法 43 -(void)textChange 44 { 45 //判斷,如果兩個文本框的內容都改變(有值)的時候,添加按鈕變成可交互的 46 self.addBtn.enabled=(self.nameFeild.text.length>0&&self.phoneFeild.text.length>0); 47 NSLog(@"通知調用的事件"); 48 } 49 50 //臨終遺言 51 -(void)dealloc 52 { 53 [[NSNotificationCenter defaultCenter] removeObserver:self]; 54 } 55 56 57 - (void)viewDidAppear:(BOOL)animated 58 { 59 // 3.主動召喚出鍵盤 60 [self.nameFeild becomeFirstResponder]; 61 // [self.nameField resignFirstResponder]; 62 } 63 64 65 - (IBAction)addBtnOnclick:(id)sender { 66 67 //點擊添加按鈕后,把數據保存到模型數組中 68 //跳轉 69 [self.navigationController popViewControllerAnimated:YES]; 70 71 //1.把當前界面文本框中的信息保存到模型中 72 YYInfoModel *info=[[YYInfoModel alloc]init]; 73 info.name=self.nameFeild.text; 74 info.phone=self.phoneFeild.text; 75 76 //2.數據逆傳(把當前控制器view的數據傳遞到上一個控制器的view中) 77 //使用代理,自定義一個代理,并使用代理傳遞數據 78 //如果代理方法存在就通知代理調用該方法,傳遞數據 79 if ([self.delegate respondsToSelector:@selector(addViewControllerDidAddBtn:contatc:)]) { 80 NSLog(@"sadafaf"); 81 [self.delegate addViewControllerDidAddBtn:self contatc:info]; 82 } 83 84 NSLog(@"dddd"); 85 } 86 @end二、效果
注銷的彈窗效果
? ??
添加信息
信息添加后返回到聯系人列表界面
轉載于:https://www.cnblogs.com/dondre/p/4094633.html
總結
以上是生活随笔為你收集整理的iOS开发UI篇—实现一个私人通讯录小应用(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux command1
- 下一篇: JVectorMap 实现中国地图