我的数组重新启动而不是附加

时间:2017-01-24 19:17:13

标签: ios swift

 class NewCourseViewController: UIViewController {

  @IBOutlet weak var courseNameTextField: UITextField!


     override func viewDidLoad() {
       super.viewDidLoad()
    }

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using       segue.destinationViewController.
    // Pass the selected object to the new view controller.

        if segue.identifier == "newClassComplete" {

if let vcSendingToo = segue.destination as?              CoursesTableViewController {

// Pass courseName to nameOfCourses Array

  // Course Name
  let courseName = courseNameTextField.text!
  vcSendingToo.nameOfCourse = courseName
  vcSendingToo.nameOfCourses.append(vcSendingToo.nameOfCourse)


}


class CoursesTableViewController: UITableViewController {

var nameOfCourses = [String]()
var nameOfCourse = ""



}

它重新启动到空白数组,而不是向同一个数组添加值。我找不到任何有助于解决我的问题的在线示例。这是工作鳍然后停止了所有的su

2 个答案:

答案 0 :(得分:0)

您是否尝试更改此行

let courseName = courseNameTextField.text!
vcSendingToo.nameOfCourse = courseName
vcSendingToo.nameOfCourses.append(vcSendingToo.nameOfCourse)

到此?

let courseName = courseNameTextField.text ?? "" //validate for nil
vcSendingToo.nameOfCourse = courseName
vcSendingToo.nameOfCourses = [courseName]

答案 1 :(得分:-3)

我将变量设为全局变量并删除了" vcSendingToo"属性。