1.在coreData中增加一个实体对象Note(+addEntity按钮,在其中添加属性,然后创建这个数据类Note)
2.创建一个继承自UITbableViewController的类,并在故事板中将原有的视图控制器删除然后创建导航视图控制器,指定为根视图
1):选中tableViewController,然后在右侧边栏中将其设置为继承自刚刚创建的那个类,然后在导航视图控制器设置为 is initial view controller
3.在表视图控制器中的代码如下
#import "ListTableViewController.h"
#import "Note.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
@interface ListTableViewController ()
//数据管理对象
@property (nonatomic, strong) NSManagedObjectContext *context;
//数据数组类存储 笔记模型
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ListTableViewController
//点击单元格跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.note = self.dataArray[indexPath.row];
//属性传值
[self.navigationController pushViewController:detailVC animated:YES];
}
//在视图即将出现时,通过数据管理对象,执行查询请求 获取数据库中的数据数组 并且赋值给当前视图控制器的数据数组,然后执行reloadData方法刷新数据
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Note"];
NSArray *array = [self.context executeFetchRequest:request error:nil];
self.dataArray = [array mutableCopy];
[self.tableView reloadData];
}
//实现添加按钮的方法,跳转到添加界面
- (void)add {
DetailViewController *detail = [[DetailViewController alloc] init];
[self.navigationController pushViewController:detail animated:YES];
self.navigationItem.title = @"新建记事本";
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//将appDelegate中的数据管理对象 取出 赋值给当前界面操作数据库需要的数据管理对象
self.context = [(AppDelegate *)[UIApplication sharedApplication].delegate managedObjectContext];
//创建一个右按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
self.navigationItem.rightBarButtonItem = item;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
return [self.dataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *resue = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue];
}
Note *note = self.dataArray[indexPath.row];
cell.textLabel.text = note.title;
return cell;
}
#pragma mark-----tableview data source-------
//1.打开编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
//2.指定哪些cell可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//3.Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//获取要删除的模型
Note *note = self.dataArray[indexPath.row];
//数据管理对象 执行delete操作 然后还有同步save
[self.context deleteObject:note];
[self.context save:nil];
//将模型从数据数组中删除
[self.dataArray removeObject:note];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
}
}
4.在添加页面的代码如下
#import "DetailViewController.h"
#import "AppDelegate.h"
#import "DetailViewController.h"
@interface DetailViewController ()
@property (nonatomic, strong) UITextField *titleTF;//显示 笔记标题
@property (nonatomic, strong) UITextView *contentView;//显示笔记内容
@property (nonatomic, strong) NSManagedObjectContext *context;
@end
@implementation DetailViewController
//声明一个方法用来创建子视图
- (void)setupSubviews {
//创建标题的label
//屏幕宽度
CGFloat screenWidth = self.view.frame.size.width;
//标题label的宽
CGFloat titleWidth = (screenWidth- 30) / 3;
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(10, 20, titleWidth, 30)];
label.text = @"标题";
[self.view addSubview:label];
//标题输入框
self.titleTF = [[UITextField alloc] initWithFrame:CGRectMake(titleWidth + 20, 20, 2 * titleWidth, 30)];
self.titleTF.borderStyle = UITextBorderStyleBezel;
self.titleTF.returnKeyType = UIReturnKeyNext;
self.titleTF.delegate = self;
[self.view addSubview:_titleTF];
//内容label
UILabel *contentlabel = [[UILabel alloc] initWithFrame: CGRectMake(10, 60, titleWidth, 30)];
contentlabel.text = @"内容";
[self.view addSubview:contentlabel];
//内容textView
self.contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, screenWidth- 20, 300)];
self.contentView.layer.borderWidth = 3;
self.contentView.layer.borderColor = [UIColor orangeColor].CGColor;
self.contentView.backgroundColor = [UIColor lightGrayColor];
self.contentView.returnKeyType = UIReturnKeyDone;
self.contentView.delegate = self;
[self.view addSubview:self.contentView];
//判断,然后赋值
if (self.note) {
self.titleTF.text = self.note.title;
self.contentView.text = self.note.content;
}
}
//放弃第一响应者
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return YES;
}
return YES;
}
//判断标题或者文本内容是否为空
- (BOOL)isEnpty {
if ([self.titleTF.text isEqualToString:@""] || [self.contentView.text isEqualToString:@""]) {
return YES;
}
return NO;
}
//保存按钮的实现
- (void)saveNote {
if ([self isEnpty]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告" message:@"?你丫傻逼吧!?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"我错了?" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
return;//这一步是防止下面的内容在为空时还创建
}
if (self.note == nil) {
self.note = [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.context];
}
//1.插入数据并且同步
self.note.title = self.titleTF.text;
self.note.content = self.contentView.text;
[self.context save:nil];
//2.返回上一界面
[self.navigationController popToRootViewControllerAnimated:YES];
}
//标题输入完成后鼠标跳到下面的文本输入框
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.titleTF resignFirstResponder];
[self.contentView becomeFirstResponder];
return NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"新建记事本";
self.navigationController.navigationBar.translucent = NO;
self.context = [(AppDelegate *)[UIApplication sharedApplication].delegate managedObjectContext];//将AppDelegate类中的managedObjectContext赋值给本类中的context
[self setupSubviews];
//创建一个保存按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveNote)];
// Do any additional setup after loading the view.
}