Home > Software design >  Sending data to another view controller but every data i send is nil or empty
Sending data to another view controller but every data i send is nil or empty

Time:01-25

all.
I am having a problem sending data.
I try to send data from AgentDetailVC to AbilitiesDetailsVC. But i cant. Every data i try to send is nil or empty string.I had managed to send before AgentListVC to AgentDetailsVC. Can y'all examine my code and help me please? Thank you a lot.

This is my AgenListVC.

import UIKit

class AgentListVC: UIViewController {
    
    var agentsReference = ValorantReferenceApp().agents
 
    
    @IBOutlet var tableView: UITableView!
    
    override func viewDidLoad() {
        
        self.tableView.rowHeight = 100
        // tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        super.viewDidLoad()
     configureNavigationBar()
        
    }
   
    
    //MARK: - Configure Navigation Bar
    func configureNavigationBar() {
        self.navigationController?.navigationBar.prefersLargeTitles = true
          if var textAttributes = navigationController?.navigationBar.titleTextAttributes {
              textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red
              navigationController?.navigationBar.titleTextAttributes = textAttributes
          }
    }


}
//MARK: - Table View Methods
extension AgentListVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return agentsReference.count
   
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! AgentCellVC
        let agent = agentsReference[indexPath.row]
        cell.agentNameLAbel.text = agent.name
        cell.agentImageview.image = agent.iconAgent
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        performSegue(withIdentifier: "toSecondVC", sender: nil)
       // tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
    
    //MARK: - Prepare Segue
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let destinationVC = segue.destination as! AgentDetailVC
        if let indexPath = tableView.indexPathForSelectedRow {
            let agent = agentsReference[indexPath.row]
            destinationVC.agentDetailVariables.choosenFlagImage = agent.agentFlagImage
            destinationVC.agentDetailVariables.choosenAgentIconImage = agent.iconAgent
            destinationVC.agentDetailVariables.choosenAgentType = agent.type.rawValue
            destinationVC.agentDetailVariables.choosenAgentName = agent.name
            destinationVC.choosenAgentAbilities = agent.abilities
   
         destinationVC.agentDetailVariables.choosenAgentAbilitiesImages = agent.agentAbilitiesImage
            destinationVC.choosenAgentVideoLink = agent.skillAbilitiesLink
 
        }
        
    }


}

This is my AgentDetailVC.

import UIKit

class AgentDetailVC: UIViewController {

    @IBOutlet var typeLabel: UILabel!
    @IBOutlet var characterImageView: UIImageView!
    @IBOutlet var flagImageView: UIImageView!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var agentTypeImageView: UIImageView!
    var agentDetailVariables = AgentDetailVariables()
    var agentsReference = ValorantReferenceApp().agents
    var choosenAgentAbilities = [String]()
    var viewController = AbilitiesDetailsViewController()
    var choosenAgentVideoLink = [String]()
   
    
    override func viewDidLoad() {
  
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        viewLoad()
        print(choosenAgentAbilities)
        
    }
    
    
//MARK: - ViewLoad Function.
    func viewLoad() {
        
        flagImageView.image = agentDetailVariables.choosenFlagImage
        characterImageView.image = agentDetailVariables.choosenAgentIconImage
        typeLabel.text = agentDetailVariables.choosenAgentType
        title = agentDetailVariables.choosenAgentName
        tableView.rowHeight = 65
        tableView.separatorStyle = .none
        agentTypeImageView.image = UIImage(named: "\(agentDetailVariables.choosenAgentType)")
       // print(agentDetailVariables.choosenAgentVideoLink)
       
    }
    

}

//MARK: - TableView Methods
extension AgentDetailVC : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return choosenAgentAbilities.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! AgentDetailsCellVC
        let agentAbilities = choosenAgentAbilities[indexPath.row]
        let skillImages = agentDetailVariables.choosenAgentAbilitiesImages[indexPath.row]
      print(agentAbilities)
    
        cell.skillNameLabel.text = agentAbilities
        cell.skillImageView.image = skillImages
 

        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         tableView.deselectRow(at: indexPath, animated: true)
        performSegue(withIdentifier: "toYoutubeVideos", sender: nil)
 
       
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toYoutubeVideos" {
            let destinationVC = segue.destination as! AbilitiesDetailsViewController
            if let indexPath = tableView.indexPathForSelectedRow {
                 let agent = agentsReference[indexPath.row]
                let link = choosenAgentVideoLink[indexPath.row]
                print(link)
                destinationVC.agentAbilitiesVideoURL = link
                destinationVC.choosenAgentSkillName = choosenAgentAbilities[indexPath.row]
            }
        }
     
    }
   
    
}

This is my AbilitiesDetailsViewController

import UIKit
import AVKit
import AVFoundation
import MediaPlayer
import MobileCoreServices
import YoutubePlayer_in_WKWebView

class AbilitiesDetailsViewController: UIViewController, AVPlayerViewControllerDelegate {

    @IBOutlet var skillDetailsLabel: UILabel!
    @IBOutlet var skillNameLabel: UILabel!
    @IBOutlet var heroIconImageView: UIImageView!
    var agentAbilitiesVideoURL = ""
    var choosenAgentSkillName = ""
 
    override func viewDidLoad() {
       
        skillNameLabel.text = choosenAgentSkillName
        super.viewDidLoad()
    
        
//playAbilitiesVideos()
        
    }
    @IBAction func playVideoUsingURL(_ sender: Any) {
            playGlobalVideo()
        }

        func playGlobalVideo() {
            guard let videoURL = URL(string: agentAbilitiesVideoURL ?? "sad") else {
                return
            }
            let player = AVPlayer(url: videoURL)
            let vc = AVPlayerViewController()
            vc.player = player
            present(vc, animated: true) {
                player.play()
            }
        }
 
    
    func playAbilitiesVideos() {
        let player = AVPlayer(url: URL(string: "https://assets.contentstack.io/v3/assets/bltb6530b271fddd0b1/bltf4e7a6525fe6ec42/625f2c7cfd9afd4b1fe300ee/C-Prowler_video.mp4")!)
                let controller = AVPlayerViewController()
                                       present(controller, animated: true) {  }
                controller.player = player
                                       addChild(controller)
                view.addSubview(controller.view)
        controller.view.frame = CGRect(x: 50 , y: 50, width: 300, height: 300)
       
                controller.player = player
                controller.showsPlaybackControls = true
                player.isClosedCaptionDisplayEnabled = false
                player.play()
    }

}

This is my struct.

struct Agent: Hashable{
  
    
    let name: String
    let type: AgentType
    let origin: String
    let abilities: [String]
    let iconAgent : UIImage
    let agentFlagImage : UIImage
    let agentAbilitiesImage: [UIImage]
   let skillAbilitiesLink: [String]
}

enum AgentType:String{
    case controller = "Controller"
    case sentinel = "Sentinel"
    case initiator = "Initiator"
    case duelist = "Duelist"
}

This is my Agents.

struct ValorantReferenceApp {
    
static var agentIconImages = AgentIconImages()
static var agentFlagImages = AgentFlagImages()
    static var agentAbilitiesImages = AgentAbilitiesImages()
    var agents: [Agent] =  [
        
        Agent(name: "Brimstone", type: .controller, origin: "United States", abilities: ["Incendiary",
                                                                                         "Stim Beacon",
                                                                                          "Sky Smoke",
                                                                                          "Orbital Strike"],
              iconAgent: agentIconImages.iconBrimstone!,
              agentFlagImage: agentFlagImages.flagBrimstone!,
              agentAbilitiesImage: agentAbilitiesImages.abilitiesBrimstone, skillAbilitiesLink:
                
                ["https://assets.contentstack.io/v3/assets/bltb6530b271fddd0b1/blte2b9eb1923ef64fa/5ecad7d0f5bd13348a6cac75/Brimstone_Q_v001_web.mp4",
                    "https://assets.contentstack.io/v3/assets/bltb6530b271fddd0b1/bltc34c3d692ea83f41/5ecad7d0177c51692beb1fe4/Brimstone_C_v001_web.mp4",
                    "https://assets.contentstack.io/v3/assets/bltb6530b271fddd0b1/bltcf4359fed083686b/5ecad7d198f79d6925dbee07/Brimstone_E_v001_web.mp4",
                    "https://assets.contentstack.io/v3/assets/bltb6530b271fddd0b1/blt3d19d83ba51eb18f/5ecad7d297b46c1911ad1868/Brimstone_X_v001_web.mp4"
                 
                                                                                                ])

]

CodePudding user response:

In AgentDetailVC you have this code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    performSegue(withIdentifier: "toYoutubeVideos", sender: nil)
}

When the user selects a Row, the first thing you've done is deselect that row. You then call performSegue.

In prepare(for segue:...) you do this:

if let indexPath = tableView.indexPathForSelectedRow {

So -- tableView.indexPathForSelectedRow will always be nil because you just set it to nil.

Call .deselectRow after you perform the segue.

  • Related