15.3 C
London
Sunday, June 9, 2024

ios – Greatest observe. Passing a customized class to a different Viewcontroller


How am i able to segue a customized class to a different Viewcontroller in Swift Correctly?
I’ve coded in javascript for some time and i am struggling to grasp what the very best observe is.

class Content material {
    var title:String
    var about:String
    var location:String

    init(information: [String: Any]) {
        self.title = information["title"] as? String ?? ""
        self.about = information["about"] as? String ?? ""
        self.location = information["location"] as? String ?? "" 
    }
}

OBS! Im at present segueing this manner which is extraordinarily verbal. If one thing modifications or if there’s a typo an error will occur.

override func put together(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "NextView" {
        let vc = segue.vacation spot as? NextViewController
        let selectedRow = sender as! Content material

            vc?.title = selectedRow.title
            vc?.about = selectedRow.about
            vc?.location = selectedRow.location
            vc?.value1 = selectedRow.value1
            vc?.value2 = selectedRow.value2
            vc?.value3 = selectedRow.value3
            vc?.value4 = selectedRow.value4
            vc?.value5 = selectedRow.value5
            vc?.value6 = selectedRow.value6
            vc?.value7 = selectedRow.value7
            vc?.value8 = selectedRow.value8 
        }
}

This is able to be my most well-liked means however i am undecided how to do that correctly

override func put together(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "NextView" {
        let vc = segue.vacation spot as! NextViewController
        let selectedRow = sender as? Content material
            
        vc.content material = selectedRow!
           
    }
}
class NextViewController: UIViewController {
    //Initializing class that has not recieved values but requires me to have a default worth for all. 
    //This could possibly be many values and complex ones. Is that this unavoidable or do I've to create a category with empty values on all ViewControllers i wish to segue a category to?
    var content material: Content material()
  
    //Is it potential and fascinating to decontruct values for simple entry?
    let { title, about, location, view1, view2, view3... } = content material

    //Or ought to values be utilized by accessing class immediately
    if content material.title == "Essential Content material" {
    }
}

What could be the very best observe for this? Any readability on this might be appreciated.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here