我想将数据从一个VC传递到另一个VC。这是我的代码。
运行程序时出现错误。您能否让我知道我在哪里做错了。
//
// ViewController.swift
// ContactPro
//
// Created by raj on 30/09/2016.
// Copyright © 2016. All rights reserved.
//
import UIKit
// Array declaration.
var contactsArray = [Contact]()
// CollectionView Protocols
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
// IBOutlets
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var contactImgView: UIImageView!
@IBOutlet weak var collectionView: UICollectionView!
var selectedIndexPath : IndexPath?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Getting the data back from the UserDefaults.
let unarchivedData = UserDefaults.standard.object(forKey: "contacts") as? Data
if let unarchivedData = unarchivedData
{
contactsArray = NSKeyedUnarchiver.unarchiveObject(with: unarchivedData) as! [Contact]
// Unarchiving the data and passing it to the appropriate textFields.
if contactsArray.count > 0
{
let singleContact = contactsArray[0]
nameLabel.text = singleContact.name
phoneLabel.text = singleContact.phone
let imagepath = imagePath(imageName: singleContact.imageName)
let image = UIImage(contentsOfFile: imagepath.path)
contactImgView.image = image
}
collectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
rounded(view: contactImgView, radius: 110)
rounded(view: button, radius: 110)
}
// MARK: - CollectionView Methods.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return contactsArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "contactCell", for: indexPath)
let imgView = cell.viewWithTag(10) as! UIImageView
let contact = contactsArray[indexPath.row]
let imageName = contact.imageName
let imagepath = imagePath(imageName: imageName)
let image = UIImage(contentsOfFile: imagepath.path)
imgView.image = image
rounded(view: imgView, radius: 50)
imgView.layer.borderColor = UIColor.white.cgColor
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let contact = contactsArray[indexPath.row]
let imageName = contact.imageName
let phone = contact.phone
let name = contact.name
let imagepath = imagePath(imageName: imageName)
let image = UIImage(contentsOfFile: imagepath.path)
nameLabel.text = name.uppercased()
phoneLabel.text = phone
contactImgView.image = image
self.selectedIndexPath = indexPath
}
// Appending image to the Document Path.
func imagePath(imageName: String) -> URL
{
let urlPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let directory = urlPath[0]
let imagePath = directory.appendingPathComponent(imageName)
return imagePath
}
// Making the view rounded.
func rounded(view: UIView, radius: CGFloat)
{
view.layer.cornerRadius = radius
view.layer.borderColor = UIColor(red:0.27, green:0.69, blue:0.60, alpha:1.00).cgColor
view.layer.borderWidth = 3
view.clipsToBounds = true
}
@IBAction func deleteAction(_ sender: Any) {
let alert = UIAlertController(title: "ALERT!",
message: "Image Will get deleted.",
preferredStyle: UIAlertControllerStyle.alert)
let Action1 = UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive) { (action) in
if let indexPath = self.selectedIndexPath{
contactsArray.remove(at: indexPath.item)
self.collectionView?.deleteItems(at: [indexPath])
self.nameLabel.text = ""
self.phoneLabel.text = ""
self.contactImgView.image = nil
}
}
let Action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default) { (action) in
}
alert.addAction(Action1)
alert.addAction(Action2)
self.present(alert, animated: true, completion: nil)
}
@IBAction func buttonCliicked(_ sender: Any) {
self.performSegue(withIdentifier: "editSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editSegue"{
let destinationVC = segue.destination as! EditVC {
destinationVC.selectedContactIndex = IndexPath.row
}
}
}
}
//
// EditVC.swift
// ContactPro
//
// Created by raj on 7/30/18.
// Copyright © 2018 . All rights reserved.
//
import UIKit
class EditVC: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var name: UITextField!
@IBOutlet weak var phoneNumber: UITextField!
var selectedContactIndex : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Edit data"
let imageView = view.viewWithTag(8) as! UIImageView
let image = contactsArray [selectedContactIndex]
let name = view.viewWithTag(9) as! UITextView
name.text = contactsArray [selectedContactIndex]
let phoneNumber = view.viewWithTag(10) as! UITextView
phoneNumber.text = contactsArray [selectedContactIndex]
}
@IBAction func Save(_ sender: Any) {
}
}
答案 0 :(得分:0)
尝试此操作,看看是否可以解决问题。 “ selectedContactIndex”在目标控制器中必须是一个变量。
async