日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

ios开发基础之通讯录系统实战-20

發布時間:2023/12/18 windows 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios开发基础之通讯录系统实战-20 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ios開發基礎之通訊錄系統實戰

基礎知識 OC 基礎
segue 的使用。delegate 代理的使用 自定義代理。面向對象思想 沙盒容器的數據持久化方案,
controller 之間的跳轉 ,登錄方法。UITableViewController的使用 導航條的使用等知識點的使用
我們使用最新的Xcode12。4 版本,配合最新的ios14.3系統和最新的mac os x系統,跑在 vmware 15虛擬機上面,需要電腦配置較高,否則比較卡頓;

實現的主要功能的截圖。有些api雖然已經過期了,但是不影響使用。 ,先把過期的學好,再學新出的api函數





// // LoginViewController.m // 20-通訊錄 // // Created by 魯軍 on 2021/2/13. //#import "LoginViewController.h" #import "MBProgressHUD+NJ.h" #import "SVProgressHUD.h" #import "ContactViewController.h"@interface LoginViewController () <UITextFieldDelegate>@property (weak, nonatomic) IBOutlet UITextField *usernameView;@property (weak, nonatomic) IBOutlet UITextField *pwdView;@property (weak, nonatomic) IBOutlet UIButton *loginBtn; - (IBAction)loginClick:(id)sender;@property (weak, nonatomic) IBOutlet UISwitch *remPwd; @property (weak, nonatomic) IBOutlet UISwitch *autoLoginSw;@end@implementation LoginViewController- (IBAction)remClick:(UISwitch *)sender {if(!sender.isOn){ // self.autoLoginSw.on=NO;[self.autoLoginSw setOn:NO animated:YES];}}- (IBAction)autoLoginClick:(UISwitch *)sender {if(sender.isOn){ // self.remPwd.on = YES;[self.remPwd setOn:YES animated:YES];}}//頁面之間的傳值 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ContactViewController *contactVc = segue.destinationViewController;[contactVc setUsername:self.usernameView.text]; }- (IBAction)loginClick:(id)sender {// [SVProgressHUD showWithStatus:@"正在登錄" maskType:SVProgressHUDMaskTypeBlack];[MBProgressHUD showMessage:@"正在登錄"];//模擬網絡延時dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(3 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{// [SVProgressHUD dismiss];[MBProgressHUD hideHUD];if([self.usernameView.text isEqualToString:@"1"] &&[self.pwdView.text isEqualToString:@"1"]){[self performSegueWithIdentifier:@"login2contact" sender:nil];NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setBool:self.remPwd.isOn forKey:@"remPwdKey"];[userDefaults setBool:self.autoLoginSw.isOn forKey:@"autoLoginKey"];[userDefaults setObject:self.usernameView.text forKey:@"usernameViewKey"];[userDefaults setObject:self.pwdView.text forKey:@"pwdViewKey"];[userDefaults synchronize];}else{//[SVProgressHUD showErrorWithStatus:@"用戶名或者密碼錯誤"];[MBProgressHUD showError:@"用戶名或者密碼錯誤"];}});} - (void)viewDidLoad {[super viewDidLoad];[self.usernameView addTarget:self action:@selector(changeValue) forControlEvents:(UIControlEventEditingChanged)];[self.pwdView addTarget:self action:@selector(changeValue) forControlEvents:(UIControlEventEditingChanged)];self.usernameView.delegate =self;//恢復狀態NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];self.remPwd.on = [ud boolForKey:@"remPwdKey"];self.autoLoginSw.on = [ud boolForKey:@"autoLoginKey"];self.usernameView.text = [ud objectForKey:@"usernameViewKey"];if(self.remPwd.isOn){self.pwdView.text = [ud objectForKey:@"pwdViewKey"];}if(self.autoLoginSw.isOn){[self loginClick:nil];}[self changeValue]; }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{[self.view endEditing:YES];}//這是文本框的代理方法 -(void)text1 { // - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. // - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder // - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end // - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called // - (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason API_AVAILABLE(ios(10.0)); // if implemented, called in place of textFieldDidEndEditing: // // - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text // // - (void)textFieldDidChangeSelection:(UITextField *)textField API_AVAILABLE(ios(13.0), tvos(13.0)); // // - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) // - (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore. // } -(void)changeValue{self.loginBtn.enabled=self.usernameView.text.length>0 && self.pwdView.text.length>0; // if(self.usernameView.text.length>0 && self.pwdView.text.length>0){ // self.loginBtn.enabled = YES; // // }else{ // // self.loginBtn.enabled = NO; // // } } /* #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 // // ContactViewController.h // 20-通訊錄 // // Created by 魯軍 on 2021/2/13. //#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface ContactViewController : UITableViewController @property(nonatomic,copy)NSString *username;@endNS_ASSUME_NONNULL_END // // ContactViewController.m // 20-通訊錄 // // Created by 魯軍 on 2021/2/13. //#import "ContactViewController.h" #import "AddViewController.h" #import "EditViewController.h"#define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"contact.data"]@interface ContactViewController () <UIActionSheetDelegate,AddViewControllerDelegate,UITableViewDataSource,UITableViewDelegate,EditViewControllerDelegate>@property(nonatomic,strong)NSMutableArray *contacts;@end@implementation ContactViewController//rang tableView jin ru bian ji mo shi hua dong sha chu - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"213");//[self.contacts removeObject:self.contacts[indexPath.row]];[self.contacts removeObjectAtIndex:indexPath.row];// [self.tableView reloadData];// dong hua xiao guo[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];[NSKeyedArchiver archiveRootObject:self.contacts toFile:kFilePath];}//cell長啥樣 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellID = @"contact_cell";UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];cell.textLabel.text = [self.contacts[indexPath.row] name];NSLog(@"dajun = %@",[self.contacts[indexPath.row] name]);cell.detailTextLabel.text=[self.contacts[indexPath.row] number];return cell;}//某一組有多少行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.contacts.count;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{NSLog(@"%ld",self.contacts.count);return 1;}- (NSMutableArray *)contacts{if(!_contacts){_contacts = [NSMutableArray array];}return _contacts; }//- (void)addViewController:(AddViewController *)addViewController withName:(NSString *)name andPhoneNumber:(NSString *)phoneNumber{ // NSLog(@"name = %@=====phoneNumber = %@",name,phoneNumber); //}// 添加聯系人的代理方法(逆傳)- (void)addViewController:(AddViewController *)addViewController withContact:(Contact *)contact{NSLog(@"name = %@=====phoneNumber = %@",contact.name,contact.number);[self.contacts addObject:contact];[self.tableView reloadData];//保存聯系人信息// NSString *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // NSString *filePath = [docPath stringByAppendingPathComponent:@"contact.data"];// NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"contact.data"]; //[NSKeyedArchiver archiveRootObject:self.contacts toFile:kFilePath];NSLog(@"%@",NSHomeDirectory());}- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{// AddViewController *addVc = segue.destinationViewController; // addVc.delegate = self; //UIViewController *vc=segue.destinationViewController;if([vc isKindOfClass:[AddViewController class]]){//設置代理AddViewController *add=(AddViewController *)vc;add.delegate=self;}else{//什么也不做EditViewController *edit =(EditViewController *)vc;edit.delegate = self;//獲取點擊的Cell的位置。indexPathNSIndexPath *path = [self.tableView indexPathForSelectedRow];Contact *con=self.contacts[path.row];edit.contact = con;}}//第二種方法。不用代理的方法 //- (void)viewWillAppear:(BOOL)animated{ // [super viewWillAppear:animated]; // [self.tableView reloadData]; // // //} //- (void)viewDidAppear:(BOOL)animated{ // // [super viewDidAppear:animated]; // [self.tableView reloadData]; //}-(void)editViewController:(EditViewController *)editViewController withContact:(Contact *)contact{// NSLog(@"%@,-----%@",contact.name,contact.number);[self.tableView reloadData];// gui dang shu zu (lian xi ren xin xi)[NSKeyedArchiver archiveRootObject:self.contacts toFile:kFilePath]; }-(void)logOut{ // UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"你確定要注銷嗎?" delegate:self cancelButtonTitle:@"qu取消" destructiveButtonTitle:@"注銷" otherButtonTitles:nil]; // [sheet showInView:self.view];NSLog(@"1231243");UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"你確定要注銷嗎?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];[actionSheetController addAction:[UIAlertAction actionWithTitle:@"注銷" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {[self.navigationController popViewControllerAnimated:YES];}]];[actionSheetController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}]];[self presentViewController:actionSheetController animated:YES completion:nil]; }//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ // // NSLog(@"%ld",buttonIndex); // // if(buttonIndex==0){ // [self.navigationController popViewControllerAnimated:YES]; // // } // // //}- (void)viewDidLoad {[super viewDidLoad];UIBarButtonItem *item0=[[UIBarButtonItem alloc] initWithTitle:@"注銷" style:UIBarButtonItemStylePlain target:self action:@selector(logOut)];//細字體self.navigationItem.leftBarButtonItem=item0;NSString *strTitle = [NSString stringWithFormat:@"%@的聯系人列表",self.username];self.navigationItem.title = strTitle;self.contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];}-(void)test1{UIBarButtonItem *item0=[[UIBarButtonItem alloc] initWithTitle:@"注銷" style:UIBarButtonItemStylePlain target:self action:nil];//細字體UIBarButtonItem *item1=[[UIBarButtonItem alloc] initWithTitle:@"注銷" style:UIBarButtonItemStyleDone target:self action:nil];// 粗字體self.navigationItem.leftBarButtonItems=@[item0,item1]; }@end // // AddViewController.h // 20-通訊錄 // // Created by 魯軍 on 2021/2/13. //#import <UIKit/UIKit.h> #import "Contact.h" @class AddViewController;// 添加協議。 進行傳值 NS_ASSUME_NONNULL_BEGIN@protocol AddViewControllerDelegate <NSObject>@optional //-(void)addViewController:(AddViewController *)addViewController withName:(NSString *)name andPhoneNumber:(NSString *)phoneNumber;-(void)addViewController:(AddViewController *)addViewController withContact:(Contact *)contact;@end@interface AddViewController : UIViewController@property(nonatomic,weak)id<AddViewControllerDelegate> delegate;@endNS_ASSUME_NONNULL_END // // AddViewController.m // 20-通訊錄 // // Created by 魯軍 on 2021/2/13. //#import "AddViewController.h" #import "Contact.h"@interface AddViewController()@property (weak, nonatomic) IBOutlet UITextField *phoneView;@property (weak, nonatomic) IBOutlet UITextField *addNameView; - (IBAction)addClick:(id)sender; @property (weak, nonatomic) IBOutlet UIButton *addBtn;@end@implementation AddViewController- (void)viewDidLoad{[self.addNameView addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];[self.phoneView addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventEditingChanged];self.addBtn.enabled=NO;// 讓姓名文本框成為第一響應者[self.addNameView becomeFirstResponder]; //自動彈出鍵盤}-(void)changeValue{self.addBtn.enabled=self.addNameView.text.length>0&&self.phoneView.text.length; }- (IBAction)addClick:(id)sender {//使用自定義的代理方法//判斷代理方法是不是能夠響應 // if([self.delegate respondsToSelector:@selector(addViewController:withName:andPhoneNumber:)]){ // // [self.delegate addViewController:self withName:self.addNameView.text andPhoneNumber:self.phoneView.text]; // // 傳統的做法 // // }if([self.delegate respondsToSelector:@selector(addViewController:withContact:)]){Contact *contact = [[Contact alloc] init];contact.name = self.addNameView.text;contact.number = self.phoneView.text;[self.delegate addViewController:self withContact:contact];}[self.navigationController popViewControllerAnimated:YES];} @end // // Contact.h // 20-通訊錄 // // Created by 魯軍 on 2021/2/14. //#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface Contact : NSObject <NSCoding>@property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *number;@endNS_ASSUME_NONNULL_END // // Contact.m // 20-通訊錄 // // Created by 魯軍 on 2021/2/14. //#import "Contact.h"@implementation Contact//dui dang - (void)encodeWithCoder:(NSCoder *)coder {[coder encodeObject:_name forKey:@"name"];[coder encodeObject:_number forKey:@"number"];}- (instancetype)initWithCoder:(NSCoder *)coder {self = [super init];if (self) {_name = [coder decodeObjectForKey:@"name"];_number = [coder decodeObjectForKey:@"number"];}return self; }@end // // EditViewController.h // 20-通訊錄 // // Created by 魯軍 on 2021/2/14. //#import <UIKit/UIKit.h> #import "Contact.h" NS_ASSUME_NONNULL_BEGIN @class EditViewController;@protocol EditViewControllerDelegate <NSObject>@optional -(void)editViewController:(EditViewController *)editViewController withContact:(Contact *)contact;@end@interface EditViewController : UIViewController @property(nonatomic,strong)Contact *contact;@property(nonatomic,weak)id<EditViewControllerDelegate> delegate;@endNS_ASSUME_NONNULL_END // // EditViewController.m // 20-通訊錄 // // Created by 魯軍 on 2021/2/14. //#import "EditViewController.h"@interface EditViewController () - (IBAction)editClick:(id)sender; @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *numberField; @property (weak, nonatomic) IBOutlet UIButton *saveBtn; - (IBAction)saveBtnClick:(id)sender;@end@implementation EditViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.nameField.text = self.contact.name;self.numberField.text = self.contact.number;}/* #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. } */- (IBAction)editClick:(UIBarButtonItem *)sender {//if([sender.title isEqualToString:@"編輯"]){if(self.saveBtn.hidden){ // dian ji bian jisender.title=@"取消";self.nameField.enabled=YES;self.numberField.enabled=YES;self.saveBtn.hidden=NO;//讓電話的文本框成為第一響應者 彈出虛擬 鍵盤[self.numberField becomeFirstResponder];}else{ // dian ji qu xiao sender.title=@"編輯";self.nameField.enabled=NO;self.numberField.enabled=NO;self.saveBtn.hidden=YES;//chong xin fu zhiself.nameField.text = self.contact.name;self.numberField.text = self.contact.number;}} - (IBAction)saveBtnClick:(id)sender {if(self.nameField.text.length==0 &&self.numberField.text.length==0){return;}self.contact.name=self.nameField.text;self.contact.number =self.numberField.text; // Contact *con =[[Contact alloc] init]; // con.name=self.nameField.text; // con.number=self.numberField.text;//判斷代理方法是不是可以響應代理方法if([self.delegate respondsToSelector:@selector(editViewController:withContact:)]){[self.delegate editViewController:self withContact:nil];}[self.navigationController popViewControllerAnimated:YES];} @end

總結

以上是生活随笔為你收集整理的ios开发基础之通讯录系统实战-20的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。