Ios 在collectionviewcontroller中单击图像时在detailviewcontroller中获取图像详细信息
My CollectionViewController包含一个图像。当我在CollectionViewController中单击图像时,它会将我带到detailviewcontroller,在那里我要编辑图像的详细信息。当我输入URL等详细信息,然后单击“上一步”按钮时,它应该从URL获取图像并替换我的CollectionViewController中的图像 我有两个问题: 当我在CollectionViewController中单击图像时,它会打开detailviewcontroller,但不会显示我单击的图像的url。 单击“上一步”按钮时,它不会将我带回CollectionViewController。 详细视图控制器Ios 在collectionviewcontroller中单击图像时在detailviewcontroller中获取图像详细信息,ios,xcode,swift,Ios,Xcode,Swift,My CollectionViewController包含一个图像。当我在CollectionViewController中单击图像时,它会将我带到detailviewcontroller,在那里我要编辑图像的详细信息。当我输入URL等详细信息,然后单击“上一步”按钮时,它应该从URL获取图像并替换我的CollectionViewController中的图像 我有两个问题: 当我在CollectionViewController中单击图像时,它会打开detailviewcontroller,但不
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
var photo: Photo!
@IBOutlet weak var textURL: UITextField!
@IBOutlet weak var textTitle: UITextField!
@IBOutlet weak var textTags: UITextField!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
let urlString = textField.text
photo.url = urlString
let DatatoImage: (NSData?) -> Void = {
if let d = $0 {
let image = UIImage(data: d)
self.imageView.image = image
} else {
self.imageView.image = nil
}
//textField.resignFirstResponder()
}
if let d = photo.data {
DatatoImage(d)
textField.resignFirstResponder()
let image = UIImage(data: d)
self.imageView.image = image
} else {
photo.loadImage(DatatoImage)
}
return true
}
}
我的masterViewController
import UIKit
let reuseIdentifier = "Cell"
class PhotosCollectionViewController: UICollectionViewController {
var photos = Array<Photo>()
override func viewDidLoad() {
super.viewDidLoad()
let photo = Photo()
photo.url = "http://www.griffith.edu.au/__data/assets/image/0019/632332/gu-header-logo.png"
photos.append(photo)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photos.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as PhotoCollectionViewCell
let photo = photos[indexPath.row]
if let d = photo.data {
let image = UIImage(data: d)
cell.imageView.image = image
photo.url = "\(photo.url)"
photo.Title = "\(photo.Title)"
} else {
photo.loadImage {
if $0 != nil {
collectionView.reloadItemsAtIndexPaths([indexPath])
}
}
}
return cell
}
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = sender as? NSIndexPath {
let photo = photos[indexPath.row]
}
if (segue.identifier == "showDetail") {
if let dvc = segue.destinationViewController as? ViewController {
let photo = Photo()
dvc.photo = photo
}
}
}
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
performSegueWithIdentifier("showDetail", sender: indexPath)
return true
}
在prepareForSegue方法中,有一行
let photo = Photo()
这将创建一个新的照片对象,它不会获取您在所选单元格中显示的照片对象。您在if-let语句中找到了正确的引用。使用该选项让photo=photos[indexPath.row]
至于后退按钮,如果按下新控制器时自动获得后退按钮,则不必编写任何代码来实现该功能。非常感谢。。谢谢你的回复。但是,当我将照片更改为照片[indexpath.row]时,我的ViewController中的Viewdidload中出现了一个错误。请查看修订,查看所有已更改的内容,以及下次如何提出更好的问题。在和->之间有两条注释需要查看。提出问题时,请确保设置代码格式,并且只包含重现问题所需的代码。您注释掉了很多代码—在提出问题时只需将其删除即可。请在提问时保持代码的格式一致。问题中的空格到处都是,有时将括号{和}放在换行符上,有时放在换行符后面。if-else语句也是如此,有时else位于对应if的}后面,有时使用两个换行符拆分if和else。改进这一点将使您更容易阅读代码并找出错误所在,从而增加有人回答您问题的机会。
let photo = Photo()