如何更新Core Data上的用户界面? [英] How to update user interface on Core Data?

查看:161
本文介绍了如何更新Core Data上的用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序与UITableView,Core Data和NSFetchedResultsController以及。我已经将数据传递给DetailViewController。我可以从DetailViewController中删除它们!在苹果的iOS Notes应用程序中,你可以看到像我想要的功能!当你从DetailViewController(例如)删除一个笔记,对象删除和Notes应用程序自动显示下一个或previos注意!我想创建这样的函数。删除当前对象后如何更新用户界面?这是我的代码!感谢`
import UIKit
import CoreData



class DetailViewController:UIViewController {

  @IBOutlet weak var containerLabel:UILabel! 

var retrieveData:NSManagedObject!
var managedObjectContext:NSManagedObjectContext!
var manager:Manager!

override func viewDidLoad(){
super.viewDidLoad()
//加载视图后进行任何其他设置。

self.containerLabel.userInteractionEnabled = false
self.containerLabel.textColor = UIColor.redColor()
self.containerLabel.alpha = 0

UIView .animateWithDuration(2.5){() - > void in
self.containerLabel.alpha = 1
}


如果let demo = self.retrieveData.valueForKey(titleField)as? String {
self.containerLabel.text = demo
}

}

override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning
//处理可以重新创建的任何资源。
}

@IBAction func backToMain(sender:AnyObject){
//返回MainTableViewController
self.dismissViewControllerAnimated(true,completion:nil)
}
@IBAction func trashButton(sender:AnyObject){

self.managedObjectContext.deleteObject(retrieveData)

do {
try self.managedObjectContext .save()
} catch {

}
self.dismissViewControllerAnimated(true,completion:nil)


} $ b b

`



像这样:





当我选择第四项从列表(例如)。和detailVC显示我选择的项目,像这样:





我想删除它们。当我删除四,然后我的 containerLabel.text 显示以前的对象从列表中。他们是在四被删除,三,两个和一之后。删除One后,我的 containerLabel.text 显示字符串





我的问题是Five!我无法删除它。示例:在iOS Notes应用程序中,如果列表中有五个对象,如我的演示应用程序。从列表中选择第四个对象时(例如)。并开始删除它们,四后删除iOS Notes应用程序显示五。并删除五(列表中的最后一个对象),然后iOS Notes应用程序显示三,两个和一个。也许问题行在这里:



if index!= 0 {
self.retrieveData = fetchedObject [index! - 1]
} else {
self.retrieveData == fetchedObject [0]
}


$ b b

解决方案

)路线。你必须把所有获取的对象传递给细节VC,如下:

  override func prepareForSegue(segue:UIStoryboardSegue,发送者:AnyObject?){

如果segue.identifier ==yourSegueIdentifier{

如果let destinationVC = segue.destinationViewController as? DetailViewController {

destinationVC.managedObjectContext = yourContext
destinationVC.retrieveData = yourManagedObject
destinationVC.arrayOfFetchedObjects = yourFetchedResultsController.fetchedObjects
//传递其他数据...

}
}
}

detailVC,写一个方法,当您按删除按钮将执行。像这样:

  @IBAction func trashButton(sender:AnyObject){

//确认你有一个数组与YourObjects
guard let fetchedObjects = arrayOfFetchedObjects as? [YourObjectType] else {return}

//获取所获取对象数组中显示对象的索引
let indexOfObject = fetchedObjects.indexOf(retrieveData)

//从上下文中删除对象
self.managedObjectContext.deleteObject(retrieveData)

do {
try self.managedObjectContext.save()

//从fetchedObjects数组中删除对象
fetchedObjects.removeAtIndex(indexOfObject)
} catch {

}

//获取对象删除后显示
如果indexOfObject!= 0 {
//我们想要表示旧注释的对象
retrieveData = fetchedObjects [indexOfObject - 1]
updateUserInterface true)

}
else {
//索引为0,因此删除的对象是最早的。现在删除之后最旧的对象采用索引0,因此只需使用此索引。还检查一个空数组。

if fetchedObjects.isEmpty {
updateUserInterface(false)
}
else {
retrieveData = fetchedObjects [0]
updateUserInterface(true)
}

}
}


func updateUserInterface(note:Bool){

switch note {

case true:

//更新用户界面
如果let demo = retrieveData.valueForKey(titleField)as? String {
self.containerLabel.text = demo
}

case false:

self.containerLabel.text =no more notes

}
}


I have an app with UITableView, Core Data and NSFetchedResultsController as well. I have passed data to the DetailViewController. And I can delete them from the DetailViewController! In the Apple's iOS Notes app, you can see such as functions as I wanted! When you delete a notes from the DetailViewController ( for example ), object deleted and Notes app automaticlly shows the next or previos notes! I want to create such as function. How update user interface after deleted current object? Here's my codes! Thanks ` import UIKit import CoreData

class DetailViewController: UIViewController {

@IBOutlet weak var containerLabel: UILabel!

var retrieveData:NSManagedObject!
var managedObjectContext:NSManagedObjectContext!
var manager:Manager!

override func viewDidLoad() {
    super.viewDidLoad()
// Do any additional setup after loading the view.

    self.containerLabel.userInteractionEnabled = false
    self.containerLabel.textColor = UIColor.redColor()
    self.containerLabel.alpha = 0

    UIView.animateWithDuration(2.5) { () -> Void in
        self.containerLabel.alpha = 1
    }


    if let demo = self.retrieveData.valueForKey("titleField") as? String {
     self.containerLabel.text = demo
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func backToMain(sender: AnyObject) {
    // Back to the MainTableViewController
    self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func trashButton(sender: AnyObject) {

    self.managedObjectContext.deleteObject(retrieveData)

    do {
    try self.managedObjectContext.save()
    } catch {

    }
    self.dismissViewControllerAnimated(true, completion: nil)


}

`

If I have 5 items on the list like so:

When I select fourth item from the list ( for example ). And detailVC shows me selected item like this:

And I want to delete them. When I delete "Four" and then my containerLabel.text shows previous objects from the list. They're after "Four" is deleted, "Three","Two" and "One" as well. After "One" is deleted my containerLabel.text shows strings

But I have left single object called as "Five"

My problem is "Five"! I can't delete it. Example: In iOS Notes App, if you have five objects on the list like my demo app. When you select fourth object from the list ( for example ). And begin deleting them, after "Four" is delete iOS Notes App shows "Five". And "Five" ( last object on the list ) is deleted and then iOS Notes App shows "Three", "Two" and "One". Maybe problem line is here:

if index != 0 { self.retrieveData = fetchedObject[index! - 1] } else { self.retrieveData == fetchedObject[0] }

解决方案

Let's take the easy (but not so elegant) route here. You'll have to pass over all the fetched objects to the detail VC like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "yourSegueIdentifier"{

    if let destinationVC = segue.destinationViewController as? DetailViewController{

        destinationVC.managedObjectContext = yourContext
        destinationVC.retrieveData = yourManagedObject
        destinationVC.arrayOfFetchedObjects = yourFetchedResultsController.fetchedObjects
        //pass over other data...

        }
    }
}

Then, in your detailVC, write a method that will be executed when you press the delete button. Something like this:

@IBAction func trashButton(sender: AnyObject) {

//make sure you have an array with YourObjects
guard let fetchedObjects = arrayOfFetchedObjects as? [YourObjectType] else {return}

//get index of the shown object in the array of fetched objects
let indexOfObject = fetchedObjects.indexOf(retrieveData)

//delete the object from the context
self.managedObjectContext.deleteObject(retrieveData)

do {
    try self.managedObjectContext.save()

    //delete the object from the fetchedObjects array
    fetchedObjects.removeAtIndex(indexOfObject)
} catch {

}

//get the object that should be shown after the delete
if indexOfObject != 0{
    //we want the object that represents the 'older' note
    retrieveData = fetchedObjects[indexOfObject - 1]
    updateUserInterface(true)

}
else{
    //the index was 0, so the deleted object was the oldest. The object that is the oldest after the delete now takes index 0, so just use this index. Also check for an empty array.

    if fetchedObjects.isEmpty{ 
         updateUserInterface(false)
     }
    else{
        retrieveData = fetchedObjects[0]
        updateUserInterface(true)
    }

    }
}


func updateUserInterface(note: Bool){

switch note{

    case true:

        //update the user interface
        if let demo = retrieveData.valueForKey("titleField") as? String {
            self.containerLabel.text = demo
        }

    case false:

        self.containerLabel.text = "no more notes"

    }
}

这篇关于如何更新Core Data上的用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆