0

How can i insert a value of textfield texts into an array of type class?

Basically, i have two view controllers one shows the list of items and code created using the following class

class Courses {

    var courseName : String
    var courseCode : Int

    init(courseName : String, courseCode : Int){
        self.courseName = courseName
        self.courseCode = courseCode
    }
}

//created array from that class

var courses : [Courses] = [Courses(courseName: "Unix Linux", courseCode: 101),
                               Courses(courseName: "ASP.Net", courseCode: 202),
                               Courses(courseName: "CISCO", courseCode: 203),
                               Courses(courseName: "Photoshop Editing", courseCode: 306)
                               ]

Second View Controller has two text field which will allow someone to add new courses to the existing array. How can i achieve that and reload the table view with new items from a different view controller

i added var courses : [Courses] = [] on the second view controller.swift file. not sure what else to do. I tried something like this

@IBAction func addCourseButton(_ sender: Any) {
        if courseName.text != "" {

            let courseCodeString = Int(courseCode.text!)
            let item = Courses(courseName: "\(courseName.text)", courseCode: courseCodeString!)

            courses.append(item)

            for i in courses {
                print(i.courseName)
            }
        }
    }

enter image description here enter image description here

heavyguidence
  • 363
  • 1
  • 8
  • 23

1 Answers1

0

There are two possible solutions to your problem.

  1. Using Protocols
  2. Using Notification Observers

In 1st case,

Add protocol in Second ViewController.swift, just above the class definition

protocol UpdateCourseAfterAddProtocol:class {

    func classListUpdated(with course: Courses)


}





class SecondViewController:UIViewController  {

    weak var dele:UpdateCourseAfterAddProtocol? = nil



@IBAction func addCourseButton(_ sender: Any) {
        if courseName.text != "" {

            let courseCodeString = Int(courseCode.text!)
            let item = Courses(courseName: "\(courseName.text)", courseCode: courseCodeString!)

          dele?.classListUpdated(with: item)
        }
    }

}

Add Segue name in Main.storyboard to "firstToSecond" or any name In first View Controller where your table View Exists (Suppose name is FirstViewController)

    class FirstViewController {


       // Bar button Action
       @IBAction func addCoursePressed(_ send:UIBarButton) {

        self.performsegue(with:"firstToSecond")
      }
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "firstToSecond" {

        let toViewController = segue.destination as! SecondViewController

        toViewController.dele = self



    }
  }
}
extension FirstViewController: UpdateCourseAfterAddProtocol {

    func classListUpdated(with course: Courses) {

        self.courses.append(course)
        self.tableView.reloadData()
     }



}
Sucharu Hasija
  • 1,096
  • 11
  • 23