实现备忘录功能代码

14 篇文章 0 订阅
13 篇文章 0 订阅
  1. #import "ViewController.h"  
  2. #import "AppDelegate.h"  
  3. #import <CoreData/CoreData.h>  
  4.   
  5.   
  6. @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>  
  7.   
  8. @property (weak, nonatomic) IBOutlet UITextField *inputTextField;  
  9. @property (weak, nonatomic) IBOutlet UITableView *taskTableView;  
  10.   
  11. @property(strong,nonatomicNSMutableArray *taskArray;  
  12.   
  13. @property(strong,nonatomicNSArray *arr;  
  14.   
  15. @end  
  16.   
  17. @implementation ViewController  
  18.   
  19. - (void)viewDidLoad {  
  20.   [super viewDidLoad];  
  21.     
  22.   //应用启动的时候加载数据库文件;  
  23.   NSManagedObjectContext *context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];  
  24.   NSFetchRequest *fetchData = [[NSFetchRequest alloc] initWithEntityName:@"Task"];  
  25.   self.arr = [context executeFetchRequest:fetchData error:nil];  
  26.     
  27.   self.taskArray = [[NSMutableArray alloc] initWithArray:[self.arr valueForKey:@"taskname"]];  
  28. }  
  29.   
  30.   
  31. #pragma mark - UITableViewDataSource  
  32. //每一个section有几个cell;  
  33. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  34.   return self.taskArray.count;  
  35. }  
  36.   
  37. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  38.     
  39.   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];  
  40.   cell.textLabel.text = [self.taskArray objectAtIndex:indexPath.row];  
  41.     
  42.   return cell;  
  43. }  
  44.   
  45. #pragma mark - 点击增加按钮  
  46. - (IBAction)addTaskButtonClick:(id)sender {  
  47.     
  48.   NSString *inputStr = [[NSMutableString alloc] initWithFormat:@"%@",self.inputTextField.text];  
  49.   inputStr = [inputStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  
  50.     
  51.   //判断如果输入的为空,则不添加;  
  52.   if ([inputStr  isEqual@""]) {  
  53.       
  54.     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的内容不能为空" preferredStyle:UIAlertControllerStyleAlert];  
  55.     [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];  
  56.     [self presentViewController:alert animated:true completion:nil];  
  57.   } else {  
  58.       
  59.     //每点击一次“确定”按钮后,就把该数据存储到CoreData中;  
  60.     [self saveToCoreData:inputStr];  
  61.       
  62.     //把一个文本存储到taskArray数组中;  
  63.     [self.taskArray insertObject:self.inputTextField.text atIndex:self.taskArray.count];  
  64.     [self.taskTableView reloadData];  
  65.       
  66.     //清空输入框;  
  67.     self.inputTextField.text = nil;  
  68.       
  69.     //点击确定后消失软键盘;  
  70.     [self.inputTextField resignFirstResponder];  
  71.       
  72.       
  73.   }  
  74.     
  75.     
  76. }  
  77.   
  78. #pragma mark - 保存数据到CoreData;  
  79. - (void) saveToCoreData:(NSString *)taskName{  
  80.     
  81.   NSManagedObjectContext *context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];  
  82.   NSManagedObject *row = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:context];  
  83.     
  84.   [row setValue:taskName forKey:@"taskname"];  
  85.   [context save:nil];  
  86.   NSLog(@"已保存到数据库");  
  87. }  
  88.   
  89.   
  90.   
  91.   
  92. #pragma mark - UIScrollViewDelegate  
  93. //滚动TableView的时候隐藏软键盘;  
  94. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{  
  95.     
  96.   [self.inputTextField resignFirstResponder];  
  97.     
  98. }  
  99.   
  100.   
  101.   
  102.   
  103. @end  

     最后的实现效果如下:


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值