查看: 5088|回复: 0

[IOS开发教程] iOS 日记app的制作过程(Objective-C)

发表于 2018-2-19 08:00:04
  1. 1.框架
  2. 我使用Realm来作为数据库的框架,还有SDAutoLayout做适配。不会用的,也没关系,这两个框架简单的很。
  3. 2.逻辑设置
  4. 日记记录的时候就记录三个数据,标题,内容,写日记的时间。这个时间精确到秒,相当于数据库的主键。我们点击以前写的日记项,也可以对其进行修改,这个时间也会修改。
  5. 3.界面设置
  6. 我先贴两个图片大家理解一下就好,反正用的控件不多
  7. 主界面一个列表心事所有日记的信息,和一个添加按钮跳转写日记的界面
  8. ![这里写图片描述](http://img.blog.csdn.net/20171218141412941?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
  9. 写日记的界面,两个TextView作为主体,三个按钮分别承当保存,取消,删除的功能
  10. ,这个删除的按钮只会在通过点击一个日记的信息列表项进入到这个界面才会显示。
  11. ![这里写图片描述](http://img.blog.csdn.net/20171218141446101?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
  12. 4.主体逻辑代码
  13. 主界面的逻辑代码
  14. ```
  15. //
  16. // MainViewController.m
  17. // Note
  18. //
  19. // Created by shanreal-iOS on 2017/12/15.
  20. // Copyright ? 2017年 shanreal.LongZhenHao. All rights reserved.
  21. //
  22. #import "MainViewController.h"
  23. #import "MainView.h"
  24. #import "MainModel.h"
  25. #import "MainTableViewCell.h"
  26. #import "DetailViewController.h"
  27. #import "NoteBean.h"
  28. @interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
  29. @property(nonatomic,strong)MainView* mainview;
  30. @property(nonatomic,strong)MainModel* model;
  31. @property(nonatomic,strong)NSMutableArray *dataArray;
  32. @end
  33. @implementation MainViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. self.navigationController.navigationBar.hidden=YES;
  38. self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
  39. self.mainview = [[MainView alloc]initWithFrame:self.view.frame];
  40. [self.mainview viewInit];
  41. [self.mainview.tableview_main setSeparatorStyle:UITableViewCellSeparatorStyleNone];
  42. self.mainview.tableview_main.bounces=NO;
  43. self.mainview.tableview_main.delegate=self;
  44. self.mainview.tableview_main.dataSource=self;
  45. [self.mainview.btn_add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
  46. [self.view addSubview:self.mainview];
  47. /*
  48. RLMResults *delete = [NoteBean allObjects];
  49. RLMRealm *realm = [RLMRealm defaultRealm];
  50. [realm transactionWithBlock:^{
  51. for (NoteBean *bean in delete) {
  52. [realm deleteObject:bean];
  53. }
  54. }];
  55. */
  56. NSString* a =[TimeStampUtil getCurrentTimeStemp];
  57. NSLog(a);
  58. }
  59. -(void)viewWillAppear:(BOOL)animated{
  60. [super viewWillAppear:animated];
  61. self.dataArray = [NSMutableArray new];
  62. RLMResults *data = [[NoteBean allObjects] sortedResultsUsingKeyPath:@"date" ascending:NO];
  63. [[RLMRealm defaultRealm] transactionWithBlock:^{
  64. for (NoteBean *bean in data) {
  65. [self.dataArray addObject:bean];
  66. }
  67. }];
  68. [self.mainview.tableview_main reloadData];
  69. NSLog(@"%d",self.dataArray.count);
  70. }
  71. -(void)addAction{
  72. DetailViewController* vc = [[DetailViewController alloc]init];
  73. vc.sort = 0;
  74. [self.navigationController pushViewController:vc animated:YES];
  75. }
  76. - (void)didReceiveMemoryWarning {
  77. [super didReceiveMemoryWarning];
  78. // Dispose of any resources that can be recreated.
  79. }
  80. #pragma mark - UITableViewDataSource
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
  82. {
  83. return 1;
  84. }
  85. - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
  86. {
  87. return self.dataArray.count;
  88. }
  89. - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  90. {
  91. NSString *cellIdentifier = [NSString stringWithFormat:@"MainTableViewCell%ld%ld", [indexPath section], [indexPath row]];
  92. MainTableViewCell *cell = (MainTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
  93. if (cell == nil) {
  94. cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  95. }
  96. NoteBean* bean = self.dataArray[indexPath.row];
  97. cell.label_title.text = bean.title;
  98. cell.label_date.text = bean.date;
  99. return cell;
  100. }
  101. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  102. return 50*MY;
  103. }
  104. #pragma mark - UITableViewDelegate
  105. - (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. [theTableView deselectRowAtIndexPath:indexPath animated:YES];
  108. NSLog(@"selected %ld row", indexPath.row);
  109. DetailViewController* vc = [[DetailViewController alloc]init];
  110. vc.sort = 1;
  111. vc.date = ((NoteBean*)self.dataArray[indexPath.row]).date;
  112. vc.title = ((NoteBean*)self.dataArray[indexPath.row]).title;
  113. vc.content = ((NoteBean*)self.dataArray[indexPath.row]).content;
  114. [self.navigationController pushViewController:vc animated:YES];
  115. }
  116. @end
  117. ```
  118. 写日期的界面的逻辑代码
  119. ```
  120. #import <UIKit/UIKit.h>
  121. #import "DetailView.h"
  122. #import "DetailModel.h"
  123. #import "NoteBean.h"
  124. @interface DetailViewController : UIViewController
  125. @property(nonatomic,assign)int sort;
  126. @property(nonatomic,strong)NSString* date;
  127. @property(nonatomic,strong)NSString* title;
  128. @property(nonatomic,strong)NSString* content;
  129. @end
  130. //
  131. // DetailViewController.m
  132. // Note
  133. //
  134. // Created by shanreal-iOS on 2017/12/15.
  135. // Copyright ? 2017年 shanreal.LongZhenHao. All rights reserved.
  136. //
  137. #import "DetailViewController.h"
  138. @interface DetailViewController ()
  139. @property(nonatomic,strong)DetailView* detailview;
  140. @end
  141. @implementation DetailViewController
  142. - (void)viewDidLoad {
  143. [super viewDidLoad];
  144. // Do any additional setup after loading the view.
  145. self.navigationController.navigationBar.hidden=YES;
  146. self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
  147. self.detailview = [[DetailView alloc]initWithFrame:self.view.frame];
  148. [self.detailview viewInit];
  149. [self.detailview.btn_save addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
  150. [self.detailview.btn_back addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
  151. [self.detailview.btn_delete addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  152. self.detailview.tf_title.text = self.title;
  153. self.detailview.tv_content.text = self.content;
  154. [self.view addSubview:self.detailview];
  155. if(self.sort == 1)
  156. self.detailview.btn_delete.hidden = NO;
  157. }
  158. -(void)viewWillAppear:(BOOL)animated{
  159. [super viewWillAppear:animated];
  160. }
  161. - (void)didReceiveMemoryWarning {
  162. [super didReceiveMemoryWarning];
  163. // Dispose of any resources that can be recreated.
  164. }
  165. -(void)deleteAction{
  166. NSLog(@"delete");
  167. NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
  168. self.date];
  169. RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
  170. RLMRealm *realm = [RLMRealm defaultRealm];
  171. [realm transactionWithBlock:^{
  172. NoteBean *bean = [beans objectAtIndex:0];
  173. [realm deleteObject:bean];
  174. }];
  175. [ShowToastView showToastView:self.view WithMessage:@"删除成功"];
  176. [self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
  177. }
  178. -(void)saveAction{
  179. NSString* title = self.detailview.tf_title.text;
  180. NSString* content = self.detailview.tv_content.text;
  181. if([title isEqualToString:@""]||title==NULL){
  182. [ShowToastView showToastView:self.view WithMessage:@"标题没写"];
  183. return ;
  184. }
  185. if([content isEqualToString:@""]||content==NULL){
  186. [ShowToastView showToastView:self.view WithMessage:@"内容没写"];
  187. return ;
  188. }
  189. NSLog(@"save %@ %@",title,content);
  190. if(self.sort == 0){
  191. NoteBean* bean = [[NoteBean alloc]init];
  192. bean.date = [TimeStampUtil getCurrentTimeStemp];
  193. bean.title = self.detailview.tf_title.text;
  194. bean.content = self.detailview.tv_content.text;
  195. RLMRealm *realm = [RLMRealm defaultRealm];
  196. [realm transactionWithBlock:^{
  197. [realm addObject:bean];
  198. }];
  199. [ShowToastView showToastView:self.view WithMessage:@"保存成功"];
  200. }else if(self.sort == 1){
  201. NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
  202. self.date];
  203. RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
  204. [[RLMRealm defaultRealm] transactionWithBlock:^{
  205. NoteBean *bean = [beans objectAtIndex:0];
  206. bean.date = [TimeStampUtil getCurrentTimeStemp];
  207. bean.title = self.detailview.tf_title.text;
  208. bean.content = self.detailview.tv_content.text;
  209. }];
  210. [ShowToastView showToastView:self.view WithMessage:@"修改成功"];
  211. }
  212. [self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
  213. }
  214. -(void)cancelAction{
  215. NSLog(@"cancel");
  216. [self.navigationController popViewControllerAnimated:YES];
  217. }
  218. @end
  219. ```
  220. 最后我奉上源代码地址
  221. http://download.csdn.net/download/z979451341/10163474
复制代码


回复

使用道具 举报