Swift 2 Nil代表

时间:2015-12-08 06:15:33

标签: ios swift swift2.1

所以我想创建一个IOS应用程序,生成一组学生,将它们添加到课程中,然后向学生展示。我可以在表格视图的列表中显示学生,但现在我想让用户触摸学生的姓名,然后转到包含该学生信息的页面(姓名最高等级)。学生课程完美无瑕,课程有效,唯一的问题是我不能让学生从一个视角到另一个视图。

这是我到目前为止所拥有的:

//
//  DataTableViewController.swift
//  assignment8
//

import Foundation
import UIKit


class DataTableViewController: UITableViewController {

  var delegate:StudentSelectionDelegate! = nil
  var students = [Student]();
  var course = Course();

  // MARK: - UITableViewDataSource

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

  }

  func didSelectStudent(controller:UITableViewController, student:Student!) {

    controller.navigationController?.popViewControllerAnimated(true)

  }

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    self.course = courseStorage.getCourse();
    self.students = course.getArrayOfStudentSortedByLastName();
    return course.count;
  }


  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let row = indexPath.row
    let currentStudent = students[row];
    if (delegate != nil) {
      delegate.didSelectStudent(self,student:currentStudent)
    }
    else {
      print ("delegate is nil :(");
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
  }

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("studentCell", forIndexPath: indexPath)

    cell.textLabel?.text = students[indexPath.row].lastName + ", " +
      students[indexPath.row].firstName;
    return cell
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    print("ping");
    if segue.identifier == "studentSegue" {
      let nextScene =  segue.destinationViewController as! studentViewController
      // Pass the selected object to the new view controller.
      if let indexPath = self.tableView.indexPathForSelectedRow {
        let selectedStudent = students[indexPath.row]
        print (selectedStudent.firstName);
        nextScene.student = selectedStudent;
      }
    }
  }
}

//
//  DataViewController.swift
//  assignment8
//

import UIKit



class DataViewController: UIViewController {

  @IBOutlet weak var dataLabel: UILabel!
  var dataObject: String = ""
  let tableData = ["One","Two","Three"];



  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.
  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.dataLabel!.text = dataObject
  }

  func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)
    -> Int {
    return self.tableData.count;
  }
}

//
//  studentViewController.swift
//  assignment8
//

import UIKit

protocol StudentSelectionDelegate {
  func didSelectStudent(controller: UITableViewController, student:Student)
}

class studentViewController: UIViewController {

  var delegate = StudentSelectionDelegate.self;

  var name = String();
  var student = Student();


  @IBOutlet weak var StudentName: UILabel!
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
  }

  func didSelectStudent(controller:UITableViewController, student:Student!) {
    student.description;
    print ("pong")
    StudentName.text = student.firstName + " " + student.lastName;

    controller.navigationController?.popViewControllerAnimated(true);
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  //  override func viewWillAppear(animated: Bool) {
  //    StudentName.text = name
  //  }

}

my storyboard 到目前为止,这是我的故事板。

因此,每当我尝试点击学生时,如果代表为零,它将打印出我已决定使用的消息。到目前为止,我已经尝试过查看SO上的所有其他答案,但没有一个能解决我的问题。

1 个答案:

答案 0 :(得分:1)

为了能够将信息从一个视图控制器发送到另一个视图控制器,您应该使用segues。看起来这就是你根据图像所做的事情。如果你不知道如何使用segue,你可以在这里找到一个好的答案:Sending data with Segue with Swift

使用segues,您将能够设置下一个视图控制器的委托:

protocol MyDelegate {
     func myFunction()
}

class FirstViewController: UIViewController, MyDelegate {

    func myFunction() {
        // do what the function does
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let secondVC = segue.destinationViewController as? SecondViewController {
            secondVC.delegate = self
        }
    }
}


class SecondViewController: UIViewController {
    var delegate: MyDelegate!
}

在你转向第二个视图控制器之前(你正在为segue做准备),你将SecondViewController的委托变量设置为self,因为FirstViewController符合MyDelegate协议,因此可以在那里使用它。现在,在SecondViewController中,您可以使用delegate.myFunction(),它将执行FirstVC函数内部的任何操作,因为FirstVC是SecondVC的委托。