• CoreData


    #import <UIKit/UIKit.h>
    
     
    
    @interface LYMAppDelegate : UIResponder <UIApplicationDelegate>
    
     
    
    @property (strong, nonatomic) UIWindow *window;
    
    //增删改查等功能通过managedObjectContext操作
    
    @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
    
    @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
    
    //绑定CoreData1.xcdatamodeld与CoreData1.sqlite数据库 
    
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
    
     
    
    - (void)saveContext;
    
    - (NSURL *)applicationDocumentsDirectory;
    
     
    
    @end
    
     #import "LYMAppDelegate.h"
    #import "UserInfo.h"
    @implementation LYMAppDelegate
    
    @synthesize managedObjectContext = _managedObjectContext;
    @synthesize managedObjectModel = _managedObjectModel;
    @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
       // [self addData];
       [self fetchData];
       // [self updataData];
       // [self deleteData];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }
    //增加数据
    -(void)addData
    {
        //获得实体对象
        UserInfo *uInfo=[NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
        //给实体赋值
        uInfo.name=@"zhangsan";
        uInfo.age=[NSNumber numberWithInt:20];
        UIImage *image=[UIImage imageNamed:@"a.png"];
        NSData *data=UIImagePNGRepresentation(image);
        uInfo.headImage=data;
        //保存到数据库
        BOOL b=[self.managedObjectContext save:nil];
        NSLog(@"%i",b);
        
    }
    //查找数据
    -(void)fetchData
    {  //创建数据库查询请求对象
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        //创建数据库中准备查询的实体
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
        //设置请求对象的实体
        [request setEntity:entity];
        //执行数据请求
        NSArray *array=[self.managedObjectContext executeFetchRequest:request error:nil];
        //遍历查询的结果
        for (UserInfo *uInfo in array) {
            NSLog(@"%@",uInfo.name);
            //后面的图片会覆盖前面的图片
            UIImage *image=[[UIImage alloc]initWithData:uInfo.headImage];
            UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
            imgView.image=image;
            [self.window addSubview:imgView];
        }
        
    }
    //删除数据
    -(void)deleteData
    {
    //查找到准备删除的对象
    //删除对象
    //保存对象
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
        [request setEntity:entity];
        
        NSArray *array=[self.managedObjectContext executeFetchRequest:request error:nil];
        UserInfo *uInfo=[array objectAtIndex:0];
        //删除数据
        [self.managedObjectContext deleteObject:uInfo];
        //保存数据
        [self.managedObjectContext save:nil];
        
        
        
        
    }
    //更新数据
    -(void)updataData
    {
      //1,查找到准备修改的对象
      //2,修改
      //3,保存
        
        //创建数据请求对象
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        //创建准备查找的实体(表)
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
        //设置请求对象的实体,也可以理解为设置请求的是哪张表
        [request setEntity:entity];
        //开始执行请求
        NSArray *array=[self.managedObjectContext executeFetchRequest:request error:nil];
        UserInfo *uInfo=[array objectAtIndex:1];//取出第二个对象
        //修改数据
        uInfo.name=@"xiaoli";
        uInfo.age=[NSNumber numberWithInt:80];
        //保存修改后的结果
        [self.managedObjectContext save:nil];
        
        
    }
    #import <UIKit/UIKit.h>
    @class LYMAppDelegate;
    #import "ListViewController.h"
    @interface RootViewController : UIViewController<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    {
        //图片拾取器,声明手机相册
        UIImagePickerController *_pickerController;
        ListViewController *_listController;
    }
    @property (strong, nonatomic) IBOutlet UITextField *nameTextFiled;
    @property (strong, nonatomic) IBOutlet UITextField *pwdTextField;
    @property (strong, nonatomic) IBOutlet UITextField *ageTextField;
    @property (strong, nonatomic) IBOutlet UIImageView *imgView;
    - (IBAction)registBtn:(id)sender;
    - (IBAction)loginBtn:(id)sender;
    
    @end
    
    #import "RootViewController.h"
    #import "LYMAppDelegate.h"
    #import "UserInfo.h"
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        //为头象的UIImageView控件添加点击的手势
        UITapGestureRecognizer *tapClick=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClickPickImage:)];
        self.imgView.userInteractionEnabled=YES;
        [self.imgView addGestureRecognizer:tapClick];
        
        
        
    }
    //#######################
    //访问手机相册
    -(void)tapClickPickImage:(UITapGestureRecognizer *)tap
    {
        _pickerController=[[UIImagePickerController alloc]init];
        _pickerController.delegate=self;
        //显示手机相册
        [self presentViewController:_pickerController animated:YES completion:^{
            
        }];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    //写入数据库
    - (IBAction)registBtn:(id)sender {
        //每个应用程序在系统中以单例形式存在,通过单例找到应用程序对象
        UIApplication *app=[UIApplication sharedApplication];
        //因为LYMAppDelegate是UIApplication得代理方所以app.delegate就可以找到LYMAppDelegate对象
        LYMAppDelegate *delegate=app.delegate;
        //插入数据
        UserInfo *uInfo=[NSEntityDescription insertNewObjectForEntityForName:@"UserInfo" inManagedObjectContext:delegate.managedObjectContext];//创建实体类
        uInfo.name=self.nameTextFiled.text;
        uInfo.password=self.pwdTextField.text;
        uInfo.age=[NSNumber numberWithInt:[self.ageTextField.text intValue]];
        UIImage *image=self.imgView.image;
        NSData *data=UIImagePNGRepresentation(image);
        uInfo.imgView=data;
        BOOL b=[delegate.managedObjectContext save:nil];
        NSLog(@"%i",b);
        
        
        
    }
    //查询数据库
    - (IBAction)loginBtn:(id)sender {
        UIApplication *app=[UIApplication sharedApplication];
        LYMAppDelegate *delegate=app.delegate;
        //创建数据库请求对象
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        //创建数据库准备请求的实体
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:delegate.managedObjectContext];
        //为请求对象设置准备请求的实体
        [request setEntity:entity];
        //开始请求数据
        NSArray *array=[delegate.managedObjectContext executeFetchRequest:request error:nil];
        for (UserInfo *uInfo in array) {
        //判断数据库中有没有和文本框中的姓名相同的名字
            if ([uInfo.name isEqualToString:self.nameTextFiled.text]) {
                //去判断密码是否相同
                if ([uInfo.password isEqualToString:self.pwdTextField.text]) {
                    NSLog(@"登陆成功");
                    _listController=[[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
                    [self presentViewController:_listController animated:YES completion:^{
                        
                    }];
                }
            }
        }
        
        
        
    }
    #pragma -mark UITextFieldDelegate
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        //设置文本框失去第一响应者
        [textField resignFirstResponder];
        return YES;
    }
    //#######################
    #pragma -mark UIImagePickerControllerDelegate
    //选中手机相册的图片是调用该方法
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
        self.imgView.image=image;
        //隐藏手机相册
        [_pickerController dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    @end
    
    
    
    #import <UIKit/UIKit.h>
    #import "MyCell.h"
    @interface ListViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
    {
        NSMutableArray *_mutArray;
    }
    @property (strong, nonatomic) IBOutlet UITableView *tbView;
    
    @end
    
    
    
    #import "ListViewController.h"
    #import "LYMAppDelegate.h"
    #import "UserInfo.h"
    @interface ListViewController ()
    
    @end
    
    @implementation ListViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        UINib *nib=[UINib nibWithNibName:@"MyCell" bundle:nil];
        [self.tbView registerNib:nib forCellReuseIdentifier:@"strIdentifier"];
    }
    //页面即将显示时调用该方法
    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        UIApplication *app=[UIApplication sharedApplication];
        LYMAppDelegate *delegate=app.delegate;
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:delegate.managedObjectContext];
        [request setEntity:entity];
        NSArray *array=[delegate.managedObjectContext executeFetchRequest:request error:nil];
        //用数据库中的数据初始化数据源数组
        _mutArray=[[NSMutableArray alloc]initWithArray:array];
        [self.tbView reloadData];
        
        
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    #pragma -mark UITableViewDataSources
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _mutArray.count;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCell *cell=[tableView dequeueReusableCellWithIdentifier:@"strIdentifier"];
        UserInfo *uInfo=[_mutArray objectAtIndex:indexPath.row];
        
        cell.nameLabel.text=uInfo.name;
        cell.ageLabel.text=[NSString stringWithFormat:@"%i",[uInfo.age intValue]];
        cell.pwdLabel.text=uInfo.password;
        cell.imgView.image=[UIImage imageWithData:uInfo.imgView];
        return cell;
    }
    //*******删除
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIApplication *app=[UIApplication sharedApplication];
        LYMAppDelegate *delegate=app.delegate;
        NSFetchRequest *request=[[NSFetchRequest alloc]init];
        NSEntityDescription *entity=[NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:delegate.managedObjectContext];
        [request setEntity:entity];
        //将数据库中查到的信息放到可变数组中
        NSMutableArray *mutArray=[[NSMutableArray alloc]initWithArray:[delegate.managedObjectContext executeFetchRequest:request error:nil]];
        UserInfo *uInfo=[mutArray objectAtIndex:indexPath.row];
        //数据库中删除
        [delegate.managedObjectContext deleteObject:uInfo];
        [delegate.managedObjectContext save:nil];
        //数据源中也要删除对象
        [_mutArray removeObjectAtIndex:indexPath.row];
        //删除页面行的信息
        [self.tbView reloadData];
    }
    #pragma -mark UITableViewDelegate
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 60;
    }
    //*******删除
    //返回编辑时的样式
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewCellEditingStyleDelete;
    }
    //*******删除
    //设置删除按钮上面的文字
    - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return @"删除";
    }
    @end
  • 相关阅读:
    四、Signalr手持令牌验证
    三、Signalr外部链接
    三、使用Fiddler劫持网络资源(手机端)
    一、数据库层搭建
    学会聊天
    weblogic实时监控开发
    jrockit静默安装笔记
    自动磁盘分区脚本
    WebsphereMQ搭建集群
    Websphere MQ Cluster
  • 原文地址:https://www.cnblogs.com/y16879w/p/4490843.html
Copyright © 2020-2023  润新知