core data swift sugue not pushing data to second view controller

喜夏-厌秋 提交于 2019-12-11 14:21:34

问题


I have a table view and I want to select an item and have it show all the data for that item in a second view controller. This is my code I am trying to use.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "showTea"){
        let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()!
        let genView:genViewController = segue.destinationViewController as genViewController
        genView.teaname = selectedIndexPath

I get the following error. 'NSIndexPath' is not convertible to 'UILabel' I am sure I am missing some thing simple but I have not found what it is.

I save the data this way.

    @IBAction func addItem(sender: AnyObject) {
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Items", inManagedObjectContext: context)
    var newItem = dataModel(entity: en!, insertIntoManagedObjectContext: context)
    newItem.name = Name.text
    newItem.type = Type.text
    newItem.amount = qty.text
    newItem.temp = temp.text
    newItem.time = time.text

    context.save(nil)

    self.navigationController?.popViewControllerAnimated(true)

}

Thanks for all the help. Not sure if this the best way to get it done but it works. genViewContorller below.

var myData: Array<AnyObject> = []
@IBOutlet var name: UILabel!
@IBOutlet var type: UILabel!
@IBOutlet var qty: UILabel!
@IBOutlet var temp: UILabel!
@IBOutlet var time: UILabel!

var row: Int = 0
var tn: String = ""
var ty: String = ""
var qtyl: String = ""
var templ: String = ""
var timel: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!
    let freq = NSFetchRequest(entityName: "Items")
    myData = context.executeFetchRequest(freq, error: nil)!
    var data: NSManagedObject = myData[row] as NSManagedObject
    var ty = data.valueForKey("type") as String
    var tn = data.valueForKey("name") as String
    var qtyl = data.valueForKey("amount") as String
    var templ = data.valueForKey("temp") as String
    var timel = data.valueForKey("time") as String
    self.name.text = "\(tn)"
    self.type.text = "\(ty)"
    self.qty.text = "\(qtyl)"
    self.temp.text = "\(templ)"
    self.time.text = "\(timel)"

I am taking the row value that is being passed via the segue and using it to get the values for each field in that row.


回答1:


You are trying to assign a NSIndexPath to a UILabel. The compiler error is pretty clear.

Note that you can't update @IBOutlets directly because they aren't set up yet (they are still nil). You will need to store your value in a property in the destination view controller, and then update your label in viewDidLoad.

  1. Add a property in genViewController to hold your value.

    var row: Int = 0
    
  2. Set the value in prepareForSegue.

    genView.row = selectedIndexPath.row
    
  3. In ViewDidLoad in genViewController set up your label:

    self.teaname.text = "I came from row \(row)"
    


来源:https://stackoverflow.com/questions/27208659/core-data-swift-sugue-not-pushing-data-to-second-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!