国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS項目-通訊錄

2019-11-14 19:03:57
字體:
來源:轉載
供稿:網友

主要是針對極客學院的通訊錄項目,知識點的整理...點擊-> 下載代碼

1,實現登陸和密碼輸入框為空時,為不可用狀態

實現方案:使用添加觀察者的方式來實現,在viewDidLoad事件當中添加觀察者,來監聽nameField和pwdField文本框內容發生改變的事件,內容改變后觸發當前類的textChange事件

- (void)viewDidLoad {    [super viewDidLoad];//添加觀察者    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdField];}

--->textChange方法 

- (void)textChange{    //修改按鈕的點擊狀態    self.loginBtn.enabled = (self.nameField.text.length && self.pwdField.text.length);}

 

2,實現頁面之間的轉跳

自storyboard出來后蘋果公司一直很推薦使用storyboard來實現項目,所以PRepareForSegue這個方法一直存在于每一個視圖控制器當中。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

這個方法的意思就是跳轉之前的準備工作,參數是UIStoryboardSegue,UIStoryboardSegue內有三個屬性,1:是identifier(標識),2:sourceViewController(原控制器),3:destinationViewController(目標控制器)

//登陸- (IBAction)loginAction {    //點擊后執行跳轉,LoginToContact為storyboard中segue的標識    [self performSegueWithIdentifier:@"LoginToContact" sender:nil];}

跳轉之前調用prepareForSegue方法

/* 一般在這里給下一個控制器傳值 這個sender是當初performSegueWithIdentifier方法傳入的sender*/- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {//1.取得目標控制器    UIViewController *contactVc = segue.destinationViewController;    //2.設置標題(傳值)    contactVc.title = [NSString stringWithFormat:@"%@的聯系人列表",self.nameField.text];}

 

3,使用第三方控件MBProgressHUD實現進度提示

在使用之前引入頭文件#import "MBProgressHUD+MJ.h",MBProgressHUD+MJ.h是對MBProgressHUD的再一次封裝,為了方便使用這個+MJ的。

//登陸- (IBAction)loginAction {    if (![self.nameField.text isEqualToString:@"jike"]) {        [MBProgressHUD showError:@"賬號不存在"];        return;    }    if (![self.pwdField.text isEqualToString:@"QQ"]) {        [MBProgressHUD showError:@"密碼錯誤"];        return;    }    //顯示蒙版(遮蓋)    [MBProgressHUD showMessage:@"努力加載中"];    //模擬2秒跳轉,以后要發送網絡請求    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        //移除遮蓋        [MBProgressHUD hideHUD];        [self performSegueWithIdentifier:@"LoginToContact" sender:nil];    });}

 

4,使用UIAlertController

自IOS8.0后UIActionSheet和UIAlertView被UIAlertController取代

- (IBAction)backAction:(id)sender {    //初始化    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否注銷?" message:@"真的要注銷嗎" preferredStyle:UIAlertControllerStyleActionSheet];    //添加按鈕    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];    [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {        [self.navigationController popViewControllerAnimated:YES];    }]];    //彈出    [self presentViewController:alert animated:YES completion:NULL];}

 

5,實現添加頁傳值

要實現的要點有:a,彈出添加頁時彈出小鍵盤,光標定位到輸入框里。來獲得更好的用戶體驗

- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    //讓姓名文本框成為第一響應者(叫出鍵盤)    [self.nameField becomeFirstResponder];}

 

b,建立Model文件夾作為模型層,在該文件夾下添加需要傳值的模型

#import <Foundation/Foundation.h>@interface JKContactModel : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *phone;@end

 

c,定義接口AddViewControllerDelegate,接口內有個方法需要傳入AddViewController和JKContactModel
@class 防止相互導入
@property為assign類型,為了防止循環引用

#import <UIKit/UIKit.h>@class AddViewController,JKContactModel;@protocol AddViewControllerDelegate <NSObject>@optional- (void)addContact:(AddViewController *)addVc didAddContact:(JKContactModel *)contact;@end@interface AddViewController : UIViewController@property (nonatomic, assign) id<AddViewControllerDelegate> delegate;@end

 

d,在AddViewController下面編寫AddAction添加按鈕點擊事件respondsToSelector判斷某個對象是否實現了某方法

//添加數據- (IBAction)AddAction {    //1.關閉當前視圖控制器    [self.navigationController popViewControllerAnimated:YES];    //代理傳值    //如果它的代理對象響應了我們這個協議方法就進行傳值    if ([self.delegate respondsToSelector:@selector(addContact:didAddContact:)]) {        JKContactModel *contactModel = [[JKContactModel alloc] init];        contactModel.name = self.nameField.text;        contactModel.phone = self.phoneField.text;        [self.delegate addContact:self didAddContact:contactModel];    }}

 

e,最后回到聯系人列表控制器ContactTableViewController來響應傳值。注意:ContactTableViewController是實現AddViewControllerDelegate這個接口的,然后再實現方法

#pragma mark - AddViewController delagate- (void)addContact:(AddViewController *)addVc didAddContact:(JKContactModel *)contact{    //1.添加數據模型    [self.contactArr addObject:contact];    //2.刷新表視圖    [self.tableView reloadData];}

 

f,還有一點要在prepareForSegue方法下設置代理為自己本身

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     AddViewController *addVc = segue.destinationViewController;    addVc.delegate = self;}

 

6,實現修改頁傳值
這個頁基本上本上面添加頁差不多....沒什么知識點好整理的...
這樣還是有一個問題,這個ContactTableViewController控制器有兩個目標控制器,這樣子需要segeue的目標控制器來判斷轉跳到哪里
修改prepareForSegue方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    id vc = segue.destinationViewController;    if ([vc isKindOfClass:[AddViewController class]]) {//如果是跳轉到添加聯系人控制器        //設置代理        AddViewController *addVc = vc;        addVc.delegate = self;    }else if ([vc isKindOfClass:[EditViewController class]]){        EditViewController *editVc = vc;        //取得選中的那一行        NSIndexPath *path = [self.tableView indexPathForSelectedRow];        editVc.contactModel = self.contactArr[path.row];        editVc.delagate = self;    }}

 

7,UITableView實現滑動刪除
UITableView有一個方法叫commitEditingStyle,通過這個方法實現刪除功能

#pragma mark - UITableView delagate- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        //1.刪除數據模型        [self.contactArr removeObjectAtIndex:indexPath.row];        //2.刷新表視圖        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];    }}

 

8,偏好設置
要實現的要點有:a,在viewDidLoad事件中加載上次配置

- (void)viewDidLoad {    [super viewDidLoad];    //讀取上次配置    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    self.nameField.text = [defaults valueForKey:UserNameKey];    self.pwdField.text = [defaults valueForKey:PwdKey];    self.rembSwitch.on = [defaults boolForKey:RmbPwdKey];    if (self.rembSwitch.isOn) {        self.pwdField.text = [defaults valueForKey:PwdKey];        self.loginBtn.enabled = YES;    }   }


b,在用戶登陸成功后保存配置

//登陸- (IBAction)loginAction {   //存儲數據    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    [defaults setObject:self.nameField.text forKey:UserNameKey];    [defaults setObject:self.pwdField.text forKey:PwdKey];    [defaults setBool:self.rembSwitch.isOn forKey:RmbPwdKey];    //設置同步    [defaults synchronize];}

 

9,對象歸檔
要實現的要點有:a, NSKeyedArchiver 必須實現NSCoding協議方法

#import "JKContactModel.h"@implementation JKContactModel/*    將某個對象寫入文件時候會調用    在這個方法中說清楚哪些屬性需要存儲 */- (void)encodeWithCoder:(NSCoder *)encoder{    [encoder encodeObject:self.name forKey:@"name"];    [encoder encodeObject:self.phone forKey:@"phone"];}/*    解析對象會調用這個方法    需要解析哪些屬性 */- (id)initWithCoder:(NSCoder *)decoder{    if (self = [super init]) {        self.name = [decoder decodeObjectForKey:@"name"];        self.phone = [decoder decodeObjectForKey:@"phone"];    }    return self;}@end


b,然后創建一個歸檔的文件名,使用到的時候將歸檔的文件加載至內存當中

#define ContactFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.data"]@property (nonatomic, strong) NSMutableArray *contactArr;@implementation ContactTableViewController- (NSMutableArray *)contactArr{    if (!_contactArr) {        //先重歸檔的文件中讀取,如果不存在,則創建一個array        _contactArr = [NSKeyedUnarchiver unarchiveObjectWithFile:ContactFilePath];        if (_contactArr == nil) {            _contactArr  = [NSMutableArray array];        }            }    return _contactArr;}

 

c,需要在數據添加、更新、刪除后都應該對數據進行歸檔

[NSKeyedArchiver archiveRootObject:self.contactArr toFile:ContactFilePath];

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莱州市| 宝清县| 门头沟区| 阳原县| 汾阳市| 米林县| 镇原县| 灵宝市| 小金县| 华宁县| 文水县| 洛川县| 徐闻县| 胶州市| 图们市| 菏泽市| 新竹市| 濮阳县| 阿尔山市| 普洱| 舒城县| 黔西县| 海兴县| 鄱阳县| 湘乡市| 龙里县| 治多县| 边坝县| 玉林市| 名山县| 隆回县| 全南县| 晋中市| 任丘市| 钟山县| 蒙自县| 同江市| 石狮市| 大埔县| 舞钢市| 兰西县|