ios開發基礎之通訊錄系統實戰
基礎知識 OC 基礎
segue 的使用。delegate 代理的使用 自定義代理。面向對象思想 沙盒容器的數據持久化方案,
controller 之間的跳轉 ,登錄方法。UITableViewController的使用 導航條的使用等知識點的使用
我們使用最新的Xcode12。4 版本,配合最新的ios14.3系統和最新的mac os x系統,跑在 vmware 15虛擬機上面,需要電腦配置較高,否則比較卡頓;
實現的主要功能的截圖。有些api雖然已經過期了,但是不影響使用。 ,先把過期的學好,再學新出的api函數
#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 setOn
:NO animated
:YES
];}}- (IBAction
)autoLoginClick
:(UISwitch
*)sender
{if(sender
.isOn
){
[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
{[MBProgressHUD showMessage
:@"正在登錄"];dispatch_after(dispatch_time(DISPATCH_TIME_NOW
,(int64_t
)(3 * NSEC_PER_SEC
)),dispatch_get_main_queue(), ^{[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{[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
{
}
-(void)changeValue
{self.loginBtn
.enabled
=self.usernameView
.text
.length
>0 && self.pwdView
.text
.length
>0;
}
@end
#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN
@interface ContactViewController
: UITableViewController
@property(nonatomic
,copy
)NSString
*username
;@endNS_ASSUME_NONNULL_END
#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
- (void)tableView
:(UITableView
*)tableView commitEditingStyle
:(UITableViewCellEditingStyle
)editingStyle forRowAtIndexPath
:(NSIndexPath
*)indexPath
{NSLog(@"213");[self.contacts removeObjectAtIndex
:indexPath
.row
];[self.tableView deleteRowsAtIndexPaths
:@[indexPath
] withRowAnimation
:UITableViewRowAnimationLeft
];[NSKeyedArchiver archiveRootObject
:self.contacts toFile
:kFilePath
];}
- (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 withContact
:(Contact
*)contact
{NSLog(@"name = %@=====phoneNumber = %@",contact
.name
,contact
.number
);[self.contacts addObject
:contact
];[self.tableView reloadData
];
[NSKeyedArchiver archiveRootObject
:self.contacts toFile
:kFilePath
];NSLog(@"%@",NSHomeDirectory());}- (void)prepareForSegue
:(UIStoryboardSegue
*)segue sender
:(id
)sender
{
UIViewController
*vc
=segue
.destinationViewController
;if([vc isKindOfClass
:[AddViewController class
]]){AddViewController
*add
=(AddViewController
*)vc
;add
.delegate
=self;}else{EditViewController
*edit
=(EditViewController
*)vc
;edit
.delegate
= self;NSIndexPath
*path
= [self.tableView indexPathForSelectedRow
];Contact
*con
=self.contacts
[path
.row
];edit
.contact
= con
;}}
-(void)editViewController
:(EditViewController
*)editViewController withContact
:(Contact
*)contact
{[self.tableView reloadData
];[NSKeyedArchiver archiveRootObject
:self.contacts toFile
:kFilePath
];
}-(void)logOut
{
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)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
#import <UIKit/UIKit.h>
#import "Contact.h"
@class AddViewController
;
NS_ASSUME_NONNULL_BEGIN
@protocol AddViewControllerDelegate
<NSObject
>@optional
-(void)addViewController
:(AddViewController
*)addViewController withContact
:(Contact
*)contact
;@end@interface AddViewController
: UIViewController
@property(nonatomic
,weak
)id
<AddViewControllerDelegate
> delegate
;@endNS_ASSUME_NONNULL_END
#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
: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
#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
#import "Contact.h"@implementation Contact
- (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
#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
#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
];self.nameField
.text
= self.contact
.name
;self.numberField
.text
= self.contact
.number
;}- (IBAction
)editClick
:(UIBarButtonItem
*)sender
{if(self.saveBtn
.hidden
){ sender
.title
=@"取消";self.nameField
.enabled
=YES
;self.numberField
.enabled
=YES
;self.saveBtn
.hidden
=NO
;[self.numberField becomeFirstResponder
];}else{ sender
.title
=@"編輯";self.nameField
.enabled
=NO
;self.numberField
.enabled
=NO
;self.saveBtn
.hidden
=YES
;self.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
;
if([self.delegate respondsToSelector
:@selector(editViewController
:withContact
:)]){[self.delegate editViewController
:self withContact
:nil
];}[self.navigationController popViewControllerAnimated
:YES
];}
@end
總結
以上是生活随笔為你收集整理的ios开发基础之通讯录系统实战-20的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。