How to make an action in one view controller do the same thing on a different view controller if the content in the first view controller row is found in one of the rows in the second view controller?

Hello,


I have a topPlayers view controller with a table, a topRookies view controller with a table, and a view controller with a table for topQuarterbacks, topRunningBacks, etc. When I swipe on a row in topQuarterbacks, it performs an action to strike a line through the text, fill the row background with color, and add an accessory. If this action is performed, I want it to search through topRookies view controller and topPlayers view controller and if a match is found, I want it to do the same action in that table. I also want to be able to swipe in topPlayers view controller and topRookies view controller and search through the rest of my tables in the view controllers for a match and if one is found to perform the same action.

Accepted Reply

I figured it out.


I changed my PlayerData class to this:


class PlayerData: Codable {
    var num: Int = 0
    var numPosPick: Int = 0
    var numRookie: Int = 0
    var name: String = ""
    var team: String = ""
    var position: String = ""
    var rosterPosition: Int = -1
    var draftPosition: Int = -1
    var isTopPlayer: Bool
    var isRookie: Bool
    
    var isRostered: Bool { return rosterPosition >= 0 }
    var isDrafted: Bool { return draftPosition >= 0 }
    
    init(num: Int, numPosPick: Int, numRookie: Int, name: String, team: String, position: String, isTopPlayer: Bool, isRookie: Bool) {
        self.num = num
        self.numPosPick = numPosPick
        self.numRookie = numRookie
        self.name = name
        self.team = team
        self.position = position
        self.isTopPlayer = isTopPlayer
        self.isRookie = isRookie
    }
}

Adding isTopPlayer and isRookie to confirm if the player is in the top 200 or if the player is a rookie or if the player is both.


I had to do a few tweaks but it works just the way I want it to now.

Replies

How do I post notification?


It makes sense to update data in permanent storage and then when the view is loaded to update its table.

To post notification:


Declare the name (at global level, outside of any class)

    public static let kMyNotification = Notification.Name("myNotification")

In the class that needs to receive notification, add in viewDidLoad


            NotificationCenter.default.addObserver(self, selector: #selector(someAction(_:)), name: .kMyNotification, object: nil)


define someAction:

@objc func someAction(_ sender: Notification) {
     // Do the update you need to do, maybe using userInfo of notification
}

In the class that sends notification, at the place where you change data:

        NotificationCenter.default.post( 
            name: .kMyNotification,
            object: self)

or (if you want to pass user data)

        NotificationCenter.default.post( 
            name: .kMyNotification,
            object: self,
            userInfo:[UsrInfo.validated: true])     // That's a dictionary where you can put data to pass



When you deinit, remove notification:

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

I figured it out.


I changed my PlayerData class to this:


class PlayerData: Codable {
    var num: Int = 0
    var numPosPick: Int = 0
    var numRookie: Int = 0
    var name: String = ""
    var team: String = ""
    var position: String = ""
    var rosterPosition: Int = -1
    var draftPosition: Int = -1
    var isTopPlayer: Bool
    var isRookie: Bool
    
    var isRostered: Bool { return rosterPosition >= 0 }
    var isDrafted: Bool { return draftPosition >= 0 }
    
    init(num: Int, numPosPick: Int, numRookie: Int, name: String, team: String, position: String, isTopPlayer: Bool, isRookie: Bool) {
        self.num = num
        self.numPosPick = numPosPick
        self.numRookie = numRookie
        self.name = name
        self.team = team
        self.position = position
        self.isTopPlayer = isTopPlayer
        self.isRookie = isRookie
    }
}

Adding isTopPlayer and isRookie to confirm if the player is in the top 200 or if the player is a rookie or if the player is both.


I had to do a few tweaks but it works just the way I want it to now.