hellosheikh
hellosheikh

Reputation: 3015

Pass object to another controller connecting through segue

hello I am new to programming. So I have some basic questions and then I need the solution of the problem which I am having.

I have created a Model Class

class Trip: NSObject {

    var tripTitle: String
    var tripSummary: String

   static var trips = [Trip]()

    init(tripTitle: String, tripSummary: String) {
        self.tripTitle = tripTitle
        self.tripSummary = tripSummary

    }

   class func addTrip(tripTitle: String, tripSummary: String){

            let t = Trip(tripTitle: tripTitle, tripSummary: tripSummary)

           trips.append(t)
        }

}

TripsDetailController

@IBAction func previewButtomPressed(sender: UIButton) {
        let tripTitle = tripTitleTxt.text!
        let tripSummary = tripSummaryTxt.text!



        Trip.addTrip(tripTitle, tripSummary: tripSummary)
       // let optionaltrip = Trip(tripTitle: tripTitle, tripSummary: tripSummary)


}



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowTripPreviewTableViewController" {


        let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
       // how can I pass the object here 
    }
}

how can I pass the object in segue

tripPreviewTableViewController

In this class I know I have to declare a variable here also but don't know what would be the datatype and also how can I access the value

and also I want to know what would be the best approach from these two

 Trip.addTrip(tripTitle, tripSummary: tripSummary)

or

let trip = Trip(tripTitle: tripTitle, tripSummary: tripSummary)

should I have to create an array and assign values or go to the second method of creating an object

Upvotes: 0

Views: 47

Answers (1)

Nikolai Volodin
Nikolai Volodin

Reputation: 86

Use the destinationViewController:

In the tripPreviewTableViewController you need to add new optional variable type of Trip:

var trip : Trip!

and now you can transfer your object with destinationViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowTripPreviewTableViewController" {

        let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
        tripPreviewTableViewController.trip = (Your Trip Object)

    }
}

//Update

First you need to declare your Trip object in TripsDetailController:

var tripToPass = Trip!

then orient it with one of these ways:

tripToPass = Trip(tripTitle:"title",tripSummary:"summary")
//or get the Trip out of your trips array:
tripToPass = Trip.trips[index]

after that you can pass the trip object:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "ShowTripPreviewTableViewController" {

        let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
        tripPreviewTableViewController.trip = tripToPass

    }
}

Upvotes: 1

Related Questions