欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 日记app的制作过程(Objective-C)

程序员文章站 2022-07-02 16:25:51
1.框架 我使用Realm来作为数据库的框架,还有SDAutoLayout做适配。不会用的,也没关系,这两个框架简单的很。 2.逻辑设置 日记记录的时候就记录三个数据,标题,内容,写日记的时间。这个时间精确到秒,相当于数据库的主键。我们点击以前写的日记项,也可以对其进行修改,这个时间也会修改。 3. ......

1.框架
我使用Realm来作为数据库的框架,还有SDAutoLayout做适配。不会用的,也没关系,这两个框架简单的很。


2.逻辑设置

日记记录的时候就记录三个数据,标题,内容,写日记的时间。这个时间精确到秒,相当于数据库的主键。我们点击以前写的日记项,也可以对其进行修改,这个时间也会修改。

3.界面设置
我先贴两个图片大家理解一下就好,反正用的控件不多
主界面一个列表心事所有日记的信息,和一个添加按钮跳转写日记的界面
![这里写图片描述](http://img.blog.csdn.net/20171218141412941?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

写日记的界面,两个TextView作为主体,三个按钮分别承当保存,取消,删除的功能
,这个删除的按钮只会在通过点击一个日记的信息列表项进入到这个界面才会显示。

![这里写图片描述](http://img.blog.csdn.net/20171218141446101?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

4.主体逻辑代码

主界面的逻辑代码

```
//
//  MainViewController.m
//  Note
//
//  Created by shanreal-iOS on 2017/12/15.
//  Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//

#import "MainViewController.h"
#import "MainView.h"
#import "MainModel.h"
#import "MainTableViewCell.h"
#import "DetailViewController.h"
#import "NoteBean.h"



@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)MainView* mainview;
@property(nonatomic,strong)MainModel* model;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden=YES;
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    
    self.mainview = [[MainView alloc]initWithFrame:self.view.frame];
    [self.mainview viewInit];
    
    [self.mainview.tableview_main setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.mainview.tableview_main.bounces=NO;
    self.mainview.tableview_main.delegate=self;
    self.mainview.tableview_main.dataSource=self;
    
    [self.mainview.btn_add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.mainview];
    /*
     RLMResults *delete = [NoteBean allObjects];
     RLMRealm *realm = [RLMRealm defaultRealm];
     
     [realm transactionWithBlock:^{
     
     for (NoteBean *bean in delete) {
     [realm deleteObject:bean];
     }
     
     }];
     */
    NSString* a =[TimeStampUtil getCurrentTimeStemp];
    NSLog(a);
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
    
    self.dataArray = [NSMutableArray new];
    RLMResults *data = [[NoteBean allObjects] sortedResultsUsingKeyPath:@"date" ascending:NO];
    
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        
        for (NoteBean *bean in data) {
            [self.dataArray addObject:bean];
        }
        
    }];
    
    [self.mainview.tableview_main reloadData];
    
    NSLog(@"%d",self.dataArray.count);
}

-(void)addAction{
    
    DetailViewController* vc = [[DetailViewController alloc]init];
    vc.sort = 0;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *cellIdentifier = [NSString stringWithFormat:@"MainTableViewCell%ld%ld", [indexPath section], [indexPath row]];
    MainTableViewCell *cell = (MainTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NoteBean* bean = self.dataArray[indexPath.row];
    
    cell.label_title.text = bean.title;
    cell.label_date.text = bean.date;
    
    return cell;
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*MY;
    
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [theTableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"selected %ld row", indexPath.row);
    
    DetailViewController* vc = [[DetailViewController alloc]init];
    vc.sort = 1;
    vc.date = ((NoteBean*)self.dataArray[indexPath.row]).date;
    vc.title = ((NoteBean*)self.dataArray[indexPath.row]).title;
    vc.content = ((NoteBean*)self.dataArray[indexPath.row]).content;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}


@end

```

写日期的界面的逻辑代码

```
#import <UIKit/UIKit.h>
#import "DetailView.h"
#import "DetailModel.h"
#import "NoteBean.h"
@interface DetailViewController : UIViewController


@property(nonatomic,assign)int sort;
@property(nonatomic,strong)NSString* date;
@property(nonatomic,strong)NSString* title;
@property(nonatomic,strong)NSString* content;

@end


//
//  DetailViewController.m
//  Note
//
//  Created by shanreal-iOS on 2017/12/15.
//  Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@property(nonatomic,strong)DetailView* detailview;


@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden=YES;
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    
    self.detailview = [[DetailView alloc]initWithFrame:self.view.frame];
    [self.detailview viewInit];
    
    [self.detailview.btn_save addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [self.detailview.btn_back addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [self.detailview.btn_delete addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.detailview.tf_title.text = self.title;
    self.detailview.tv_content.text = self.content;
    
    [self.view addSubview:self.detailview];
    
    if(self.sort == 1)
        self.detailview.btn_delete.hidden = NO;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)deleteAction{
    
    NSLog(@"delete");

    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
                         self.date];
    RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
    RLMRealm *realm = [RLMRealm defaultRealm];
    [realm transactionWithBlock:^{
        NoteBean *bean = [beans objectAtIndex:0];
        [realm deleteObject:bean];
        
    }];
    [ShowToastView showToastView:self.view WithMessage:@"删除成功"];

    [self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
}

-(void)saveAction{
    
    NSString* title = self.detailview.tf_title.text;
    NSString* content = self.detailview.tv_content.text;
    
    if([title isEqualToString:@""]||title==NULL){
        [ShowToastView showToastView:self.view WithMessage:@"标题没写"];
        return ;
    }
    
    if([content isEqualToString:@""]||content==NULL){
        [ShowToastView showToastView:self.view WithMessage:@"内容没写"];
        return ;
    }
    
    
    NSLog(@"save %@ %@",title,content);
    if(self.sort == 0){
        NoteBean* bean = [[NoteBean alloc]init];
        bean.date = [TimeStampUtil getCurrentTimeStemp];
        bean.title = self.detailview.tf_title.text;
        bean.content = self.detailview.tv_content.text;
        
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm transactionWithBlock:^{
            [realm addObject:bean];
        }];
        
        [ShowToastView showToastView:self.view WithMessage:@"保存成功"];
        
    }else if(self.sort == 1){
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
                             self.date];
        RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
        
        [[RLMRealm defaultRealm] transactionWithBlock:^{
            NoteBean *bean = [beans objectAtIndex:0];
            bean.date = [TimeStampUtil getCurrentTimeStemp];
            bean.title = self.detailview.tf_title.text;
            bean.content = self.detailview.tv_content.text;

        }];
        [ShowToastView showToastView:self.view WithMessage:@"修改成功"];
        
        
    }

    
    [self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
}

-(void)cancelAction{
    NSLog(@"cancel");
    [self.navigationController popViewControllerAnimated:YES];
}


@end

```


最后我奉上源代码地址
http://download.csdn.net/download/z979451341/10163474