[IOS开发进阶与实战]第一天:CoreData制作的第一个简单APP(添加,显示,删除 数据)

博主仿写App花了近一下午,中途因疏忽致App无法运行。为了解CoreData使用建了Empty app并连接,还建了storeboard与代理链接,添加viewController等。此外,提到tabbar item记录及NSFetchedResultsController控制类等重要知识点。

麻雀虽小,五脏俱全,今天仿写这个APp,用了我将近一下午的时候,中途还因为一点疏忽大意导致自己的app竟然无法运行.

好了,注意的地方我会在写的时候提示.

come ON ,~~baby.

1.因为我想更加清晰的了解这个  CoreData的使用,所以我就建立了一个Empty app.然后进行连接.

特别需要注意的是:我需要使用 coreData和storeBoard,所以请选上.


2.我们再建立一个storeboard ,然后建立 与代理的链接.

我们先删除了  app didfinishLaunching...中的代码,只保留了 return true. 因为store 使用的是更加方便的箭头去指向 root view,所以就没那么的麻烦了.

当然,新建立的 storeBoard是空白的,我们必须添加viewController.我们就拖拽一个 navigationViewController .哈哈,你会发现后面还带了一个 UitableViewController.不错.

下一步就是关键的连接操作了.

由于我在学习的时候错误的改变了bundle identifier 的值,导致我最后黑屏了~

.由于今天的代码还有一些问题,我先说说今天一些重要的知识点吧

3.如果对我们的tabbar item进行记录,再次启动的时候还是我们原来的选择项?

首先定义一个 常量,和一个枚举,便于我们进行记录和使用

#define kSelectedTabDefaultsKey @"Selected Tab"

enum {
    kByName,
    kBySecretIdentity,
};


如果对我们的选择进行记录呢 ?

那就要实现协议并进行记录了

#pragma mark - UITabBarDelegate Methods//我们首先实现了 UITabBarDelegate 方法

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSUInteger tabIndex = [tabBar.items indexOfObject:item];
    [defaults setInteger:tabIndex forKey:kSelectedTabDefaultsKey];
    
    [NSFetchedResultsController deleteCacheWithName:@"Hero"];
    _fetchedResultsController.delegate = nil;
    _fetchedResultsController = nil;

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Error performing fetch: %@", [error localizedDescription]);
    }
    
    [self.heroTableView reloadData];
}

-(void)viewdidLoad 中加载我们的常量

- (void)viewDidLoad
{
    [super viewDidLoad];
//    UITabBarItem *item = [self.heroTapBar.items objectAtIndex:0];
//    
//    [self.heroTapBar setSelectedItem:item];
    

    // 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.leftBarButtonItem = self.editButtonItem;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSInteger selectedTab = [defaults integerForKey:kSelectedtabDefaultsKey];
    
    UITabBarItem *item = [self.heroTapBar.items objectAtIndex:selectedTab];
    
    [self.heroTapBar setSelectedItem:item];
     NSLog(@"解析成功 %d",selectedTab);
    
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"error loading" message:@"view did load error" delegate:self cancelButtonTitle:@"e ,,,," otherButtonTitles:nil];
        
        [alertView show];
        
        
        
    }
    
    //Users Defaluts 来记忆~
    
}

4.如何创建一个

NSFetchedResultsController 控制类?  实现

FetchedResultsController Property

#pragma mark - FetchedResultsController Property

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];//创建请求
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; //获得我们的代理
    NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];//通过代码获得上下文.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Hero"
                                              inManagedObjectContext:managedObjectContext];//创建实体描述.
    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];
    //设置实体属性
    NSUInteger tabIndex = [self.heroTabBar.items indexOfObject:self.heroTabBar.selectedItem];
    if (tabIndex == NSNotFound) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        tabIndex = [defaults integerForKey:kSelectedTabDefaultsKey];
    }
    
    NSString *sectionKey = nil;
    switch (tabIndex) {
            // Notice that the kByName and kBySecretIdentity Code are nearly identical - refactoring opportunity?
        case kByName: {
            NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
            NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"secretIdentity" ascending:YES];
            NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];
            sectionKey = @"name";
            break;
        }
        case kBySecretIdentity:{
            NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"secretIdentity" ascending:YES];
            NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
            NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,
                                        sortDescriptor2, nil];
            [fetchRequest setSortDescriptors:sortDescriptors];
            sectionKey = @"secretIdentity";
            break;
        }
    }
    
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKey cacheName:@"Hero"];
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}



 



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值