与 segue 一起传递数据
Posted
技术标签:
【中文标题】与 segue 一起传递数据【英文标题】:Passing data along with segues 【发布时间】:2015-06-06 22:24:25 【问题描述】:问题是:我试图将一些数据从一个 ViewController 传递到另一个 InfoViewController。它看起来像这样:
运行,应用程序,它看起来像这样:
除了这两个视图控制器之外,我在这个应用程序中还有其他一些视图控制器,它们的 Segues 运行良好,但是,当我点击单元格时,它确实转到 InfoViewController,但信息确实不更新。
这是我的代码:
import UIKit import CoreData
protocol sendNameDelegate
func sendName(name: String)
protocol sendImageDelegate
func sendImage(image: UIImage)
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, sendDetailsToMVCDelegate, NSFetchedResultsControllerDelegate, NSCoding
@IBOutlet weak var tableView: UITableView!
var namesListArray:[String] = []
var imagesListArray:[UIImage] = []
var delegateName: sendNameDelegate?
var delegateImage: sendImageDelegate?
required init(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
if let namesList = aDecoder.decodeObjectForKey("namesListArray") as? [String]
namesListArray = namesList
else
namesListArray = [String]()
if let imagesList = aDecoder.decodeObjectForKey("imagesListArray") as? [UIImage]
imagesListArray = imagesList
else
imagesListArray = [UIImage]()
override func encodeWithCoder(aCoder: NSCoder)
aCoder.encodeObject(self.namesListArray, forKey: "namesListArray")
aCoder.encodeObject(self.imagesListArray, forKey: "imagesListArray")
override func viewDidLoad()
let dir = getUserDir()
let archiveName = "\(dir)/iRecipeList-namesListArray"
if let loaded: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithFile(archiveName)
self.namesListArray = (loaded as? [String])!
let archiveImage = "\(dir)/iRecipeList-imagesListArray"
if let loaded: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithFile(archiveImage)
self.imagesListArray = (loaded as? [UIImage])!
override func viewDidAppear(animated: Bool)
func sendDetailsToMVC (name: String, image: UIImage)
namesListArray.append(name)
imagesListArray.append(image)
let dir = getUserDir()
let archiveName = "\(dir)/iRecipeList-namesListArray"
NSKeyedArchiver.archiveRootObject(namesListArray, toFile: archiveName)
let archiveImage = "\(dir)/iRecipeList-imagesListArray"
NSKeyedArchiver.archiveRootObject(imagesListArray, toFile: archiveImage)
tableView.reloadData()
func getUserDir() -> String
let userDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
return userDir[0] as! String
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return namesListArray.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let row = indexPath.row
let name = namesListArray[row]
println("\(row)/")
let image = imagesListArray[row]
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
cell.textLabel?.text = name
cell.imageView!.image = image
return cell
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
var row = indexPath.row
var name = namesListArray[row]
delegateName?.sendName(name)
var image = imagesListArray[row]
delegateImage?.sendImage(image)
performSegueWithIdentifier("goToInfoVC", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "newReciep"
var vc = segue.destinationViewController as! DetailsViewController
vc.delegateDetails = self
还有:
import UIKit
import CoreData
class InformationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, sendNameDelegate, sendImageDelegate
@IBOutlet var recipeNameLabel: UILabel!
@IBOutlet var recipeImageView: UIImageView!
@IBOutlet var RecipeHowToDo: UILabel!
@IBOutlet var recipeIngredientsTableView: UITableView!
var ingredientsListArray = [String]()
func sendName(name: String)
recipeNameLabel.text = name
func sendImage(image: UIImage)
recipeImageView.image = image
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return ingredientsListArray.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let row = indexPath.row
let ingredient = ingredientsListArray[row]
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
cell.textLabel!.text = ingredient
return cell
override func viewDidLoad()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
我似乎无法将此信息正确发送到最后一个视图控制器。有人有什么想法吗?
PS:抱歉图片太多了,只是觉得有用。
【问题讨论】:
【参考方案1】:在为它们分配任何值之前,您当前正在调用 sendName 和 sendImage 委托。这就是为什么他们没有访问您的 InformationViewController。你应该这样做:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "newReciep"
var vc = segue.destinationViewController as! DetailsViewController
vc.delegateDetails = self
else if(segue.identifier == "goToInfoVC")
var vc = segue.destinationViewController as! InformationViewController
let row = self.tableView.indexPathForSelectedRow()?.row
var name = namesListArray[row]
var image = imagesListArray[row]
vc.name = name
vc.image = image
然后在 infoViewController ViewDidLoad 方法中,您可以将 name 和 image 的值设置为您的视图。
【讨论】:
明白了!对于刚开始的人来说,这有点令人困惑,因为有很多方法可以设置所有内容。我一回到家,我会尽力让你知道! 我按照你说的尝试了,当我点击单元格时没有任何反应:/我该怎么办? 对不起,我迟到了。你解决了吗? 是的,我做到了!我通过 prepareForSegue 设置了我需要的信息。我现在的业力是了解更多有关 CoreData 的信息,以便我可以更轻松地管理这些信息。以上是关于与 segue 一起传递数据的主要内容,如果未能解决你的问题,请参考以下文章