认为没有将数据传递给第二个VC

时间:2017-02-24 14:47:10

标签: ios swift cocoa-touch

我有一个视图控制器,用课程场地和日期字段填充tableView。我试图让选定的行将记录传递给第二个视图控制器,我想在那里显示场地标题。

代码显示没有错误,除了第二个视图控制器中的venueTitle的UILabel没有显示场地标题。

1st ViewController -

import UIKit
import CloudKit

class CoursesVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var coursesTable: UITableView!


var coursesArray: Array<CKRecord> = []

var selectedCourseIndex: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    coursesTable.delegate = self
    coursesTable.dataSource = self
    fetchCourses()

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return coursesArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath)
    let courseRecord: CKRecord = coursesArray[indexPath.row]

    cell.textLabel?.text = courseRecord.value(forKey: "courseVenue") as? String

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy"

    let CSDate = dateFormatter.string(from: courseRecord.value(forKey: "courseDate") as! Date)
    cell.detailTextLabel?.text = CSDate

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40.0
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedCourseIndex = indexPath.row
        self.performSegue(withIdentifier: "showMapDetail", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showMapDetail" {
        let coursesMap = segue.destination as! CourseMapVC
        if let index = selectedCourseIndex {
            coursesMap.courseRecord = coursesArray[index]
        }
    }
}

func fetchCourses() {
    let container = CKContainer.default()
    let publicDatabase = container.publicCloudDatabase
    let predicate = NSPredicate(format: "TRUEPREDICATE")

    let query = CKQuery(recordType: "Courses", predicate: predicate)
    query.sortDescriptors = [NSSortDescriptor(key: "courseDate", ascending: true)]

    publicDatabase.perform(query, inZoneWith: nil) { (results, error) -> Void in

        if error != nil {

            print("error fetch notes \(error)")

        } else {

            print("Success")

            for result in results! {
                self.coursesArray.append(result )
            }

            OperationQueue.main.addOperation({ () -> Void in
                self.coursesTable.reloadData()
                self.coursesTable.isHidden = false
            })
        }
    }
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

}

和第二个视图控制器 -

import UIKit
import CloudKit
import MapKit


class CourseMapVC: UIViewController {


@IBOutlet weak var venueTitle: UILabel!

@IBOutlet weak var mapView: MKMapView!

var courseRecord: CKRecord!


override func viewDidLoad() {
    super.viewDidLoad()


    if let course = courseRecord {
        venueTitle.text = course.value(forKey: "courseTitle") as? String
    }

   let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)


}


}

我并不担心CLLocation,我更关心的是传递的信息或者现阶段没有。非常感谢任何帮助。

enter image description here

3 个答案:

答案 0 :(得分:1)

听起来像UI布局问题而不是Storyboard。您是否尝试过调试视图?标签可能被裁剪

答案 1 :(得分:0)

您的segue标识符可能不正确。尝试使用segue目标控制器的可选转换为CourseMapVC作为条件。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    switch segue.destination {
    case let controller as CourseMapVC:
        if let index = selectedCourseIndex {
            controller.courseRecord = coursesArray[index]
        }
    default:
        break
    }
}

答案 2 :(得分:0)

今天早上一双清新的眼睛(和大脑),我已经解决了Segue问题。

我需要  1.再次调用Array(对tableView执行了此操作)  2.从所选行中获取字段courseVenue  3.指定值作为String

传递

感谢所有指针,这里是修改后的代码。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showMapDetail" {
        let destinationController = segue.destination as! CourseMapVC
        if let indexPath = tableView.indexPathForSelectedRow {
            let courses: CKRecord = coursesArray[indexPath.row]
            let coursesVenueCell = courses.value(forKey: "courseVenue")
            destinationController.courseTitle = coursesVenueCell as! String
        }
    }
}