iOS 日記app的製作過程(Objective-C),iosobjective-c

來源:互聯網
上載者:User

iOS 日記app的製作過程(Objective-C),iosobjective-c

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.