我正在尝试将图像从我的集合视图(PortfolioView.swift)传递到另一个视图控制器(PortfolioDetail.swift)。我的PortfolioView.swift代码读取:
import UIKit
class Portfolio View: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var array = [String] = []
var name : AnyObject? {
get {
return NSUserDefaults.standardUserDefaults().objectForKey("name")
}
set {
NSUserDefaults.standardUserDefaults.setObject(newValue!, forKey: "name")
NSUserDefaults.standardUserDefaults().synchronize()
}
}
override func viewDidLoad() {
super.viewDidLoad()
array = ["PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7",
"PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7",
"PortImage1","PortImage2","PortImage3","PortImage4","PortImage5","PortImage6","PortImage7"]
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundImage.png")!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
cell.ImageView.image = UIImage(named: array[indexPath.row])
cell.ImageView.layer.cornerRadius = cell.ImageView.frame.size.width / 2
cell.ImageView.clipsToBounds = true
cell.ImageView.layer.borderWidth = 2.0
cell.ImageView.layer.borderColor = UIColor.whiteColor().CGColor
return cell
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
name = UIImage(named: array[indexPath.row])
}
}
我的PortfolioDetail.swift代码为:
import UIKit
class PortfolioDetail: UIViewController {
@IBOutlet var DetailImageView: UIImage!
@IBOutlet var DetailLabel: UILabel!
var name: AnyObject? {
get {
return NSUserDefaults.standardUserDefaults().objectForKey("name")
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundImage.png")!)
DetailImageView.image = UIImage(named: name as! String)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
当我运行这个应用程序时,我能够显示我的图像数组,但是当我点击其中一个图像时,它不会在我的其他VC中显示图像。我认为我的问题在于以下几行:
DetailImageView.image = UIImage(named: name as! String)
答案 0 :(得分:0)
您要在String
中保存name = UIImage(named: array[indexPath.row])
,然后尝试将其用作name = array[indexPath.row]
- 这不起作用,因为这两种类型不兼容。使事情有效的最简单方法是改变
NSUserDefaults
到
name
这样,您就可以在PortfolioDetail
中保存图片的名称,并且以后可以根据它加载图片。
现在,正如评论中所说的那样 - 在视图控制器之间传递数据这种方式并不是一个很好的设计选择。最好的方法是直接传递presentViewController
- 有两种方法可以做到这一点,具体取决于你如何移动到override func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
if (segue.identifier == "YourSegueIdentifier") {
let portfolioDetail = segue.destinationViewController as! PortofolioDetail
portfolioDetail.name = self.name
}
}
视图控制器 - 通过segue或"直接" (即在代码中自己实例化并调用let portfolioDetailVC = PortfolioDetail()
portoflioDetailVC.name = self.name
self.presentViewController(portfolioDetailVC, animated: true, completion: nil)
)。
如果您使用segue执行此操作,则应添加(如果您没有)此方法:
get
如果你直接实例化它,它应该是这样的:
set
您还需要删除两个视图控制器中name
变量的自定义{{1}}和{{1}}个错误。