-3

i am new in swift and my question maybe confuse you but i try to explaine that clearly. i want make weather app. that i have one view controller in my story. in my view controller, i have 2 view that i make them in seperate xib file. one view has a label for show the name and a imageview for show the condition weather of the selected city and another view is collection view that show many city for selecting. i save my city information in a class and make a model for city. this is my model file:

struct City {        
    private(set) public var code: String
    private(set) public var fName: String
    private(set) public var eName: String
    private(set) public var image: String        

    init (code: String, fName: String, eName: String, image: String) {
        self.code = code
        self.fName = fName
        self.eName = eName
        self.image = image
    }        
}

and below code is my class that i save many city info in array;

public class Publics {    

    static let instance = Publics()          

    private let Cities = [
        City(code: "143127", fName: "a", eName: "A", image: "https://media.is48.jpg/3") ,
        City(code: "143083", fName: "b", eName: "B", image: "http://www.ie_650_365.jpg") ,
        City(code: "121801", fName: "c", eName: "C", image: "https://www.weeeh.jpg?13112") ,
        City(code: "418863", fName: "d", eName: "D", image: "https://i.pinf7c0ec2c8617.jpg") 
]
}

i show my array in collection City correctly, now i want when i select one city, the eName and image of my selected city show in label and imageview of first view. i write below code for save selected city information in "city" var but i dont know how can i send this info to another file:

   var city = City(code: "", fName: "", eName: "", image: "")

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        city = Publics.instance.getCities()[indexPath.row]

    }

what should i do? (im sorry for long description)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3805066
  • 13
  • 1
  • 5
  • 2
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Magnas Jan 26 '19 at 16:08
  • You don't send info to a file, you send it to an object (big difference). What object do you want to send it to and how is that object related to the one that contains the info? – Phillip Mills Jan 26 '19 at 16:43
  • all of the views is in one view controller. i saw your link and i think notification is good but i could not use from that. @Magnas – user3805066 Jan 26 '19 at 20:57
  • my object is city that i wrote in didSelectItemAt function. @PhillipMills – user3805066 Jan 26 '19 at 20:59

2 Answers2

0

There are many ways to send data between views 1 - dependency injection 2- NSNotification(can use rxswift also) 3-delegates 4-closures 5-store in user defaults (not preferred) 6-singleton object (not prefered)

Ahmed Nour
  • 186
  • 1
  • 5
-1

One way to do this is to use Notifications. When the user selects a city from the collection view, the collection view (or its controller) could post a notification to the default NotificationCenter saying that a new city was selected.

Any other object that wishes to know whenever the selection changes can listen for that same notification. When it observes that notification has happened, it can do whatever it needs to.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • i write below code for didSelectItemAt function but i have error: NotificationCenter.default.post(name: Notification.Name(rawValue: "tts"), object: nil, userInfo: city ) and below code in awakeFromNib of other view: NotificationCenter.default.addObserver(self, selector: #selector(self.showcity(_:)), name: NSNotification.Name(rawValue: "tts"), object: nil) and this func: @objc func showcity(_ notification: NSNotification) { print(notification.userInfo ?? "") } – user3805066 Jan 26 '19 at 21:02
  • Cannot convert value of type 'City' to expected argument type '[AnyHashable : Any]?' and i change to below code but retuen empty: NotificationCenter.default.post(name: Notification.Name(rawValue: "tts"), object: nil, userInfo: city as? [AnyHashable : Any]) – user3805066 Jan 26 '19 at 21:37
  • The `userInfo` parameter has to be of type `Dictionary`. You can put the `City` object into a `Dictionary` with a key of `"City"` and then in your function that gets called when the `Notification` fires, get it back out of the `userInfo` `Dictionary` using the same key. – user1118321 Jan 26 '19 at 21:57
  • i use your explain and it was true. thanks. i write this : let city = Publics.instance.getCities()[indexPath.row] let yyy:[String: City] = ["city": city] NotificationCenter.default.post(name: NOTIF_USER, object: nil, userInfo: yyy) and in function : if let dict = notification.userInfo as NSDictionary? { print(dict["city"]) } this code print all field of city. now how can i print one field of that like "code" ????? – user3805066 Jan 26 '19 at 22:12