1

I am beginner in iOS development and I have implemented following screen using UICollectionView :

enter image description here

CollectionViewCell File Code is:

import UIKit

class EventCell: UICollectionViewCell{

var classEvent: Event?

@IBOutlet weak var eventTitle: UILabel!
@IBOutlet weak var eventTeams: UILabel!
@IBOutlet weak var eventTime: UILabel!
@IBOutlet weak var eventTeamOneImage: UIImageView!
@IBOutlet weak var eventTeamTwoImage: UIImageView!

@IBOutlet weak var leaderboardButton: UIButton!

var datasourceItem: Any?{
    didSet{
        guard let event = datasourceItem as? Event else { return }
        classEvent = event

        eventTitle.text = "Match \(event.matchNo) (\(event.matchStage))"
        eventTeams.text = "\(event.teamOne.nameAttr) vs \(event.teamTwo.nameAttr)"
        eventTime.text = "\(event.getEventLockTimeAsString())"
        eventTeamOneImage.loadImageUsingCache(withUrl: event.teamOne.flagPhoto)
        eventTeamTwoImage.loadImageUsingCache(withUrl: event.teamTwo.flagPhoto)
        leaderboardButton.addTarget(self, action: #selector(handleLeaderBoardClick), for: .touchUpInside)
    }
}

@IBAction func leagueButton(_ sender: Any) {
}

weak var delegate: HomeControllerDelegate?
func handleLeaderBoardClick() {
    if let matchId = classEvent?.id {
        print(matchId)
        delegate?.clickOnLeaderBoard(matchId: matchId)
    }
}   
 }

Now on click on Leaderboard button(icon with 1,2,3) I would like to open new LeaderBoard Controller and pass matchId which is classEvent.id

How can I pass values to the new controller? And what is the best way to do that.

Amit
  • 4,837
  • 5
  • 31
  • 46
Hardik Gondaliya
  • 316
  • 3
  • 12
  • Please check my answer – Saurabh Jain Aug 21 '17 at 18:25
  • Please refer these two links : https://stackoverflow.com/questions/31075116/passing-data-between-two-viewcontrollers-delegate-swift and https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Amit Aug 21 '17 at 18:26

2 Answers2

2

You can pass the match Id via segue:

In LeaderBoard Controller set a property:

var matchId:Int?

Set a segue between the controller and add an identifier: On Click leaderboard button:

self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)

Add the segue method:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if segue,identifier == "yourSegueIdentifier" {
        let destinationVC = segue.destinationViewController as LeaderbardController
        destinationVC.matchId = classEvent.id
        }
}
    }
Saurabh Jain
  • 1,688
  • 14
  • 28
  • @IBAction func leaderboardButton(_ sender: UIButton) { self.performSegueWithIdentifier("leaderboardSegue", sender: self) } on leaderboardButton is showing error Value of type 'EventCell' has no member 'performSegueWithIdentifier' – Hardik Gondaliya Aug 21 '17 at 18:26
  • because it's a controller method, Please add this method into your controller – Saurabh Jain Aug 21 '17 at 18:31
  • what about prepareForSegue function? classEvent is defined in EventCell – Hardik Gondaliya Aug 21 '17 at 18:33
  • when you call the self.performSegueWithIdentifier("leaderboardSegue", sender: self) then the prepare for segue called, it is also view controller method.. define it in your view controller – Saurabh Jain Aug 21 '17 at 18:35
  • yes I defined methods in view controller but how to get classEvent.id as I dont have access of classEvent in view controller – Hardik Gondaliya Aug 21 '17 at 18:43
  • Please refer for this tutorial for segue information https://makeapppie.com/2016/06/27/using-segues-and-delegates-for-navigation-controllers-in-swift-3-0/ – Saurabh Jain Aug 22 '17 at 03:50
-1

Three easy steps to get what u want:

  1. Make a BaseViewController class a subclass of UiViewController. This class would be the alternate of UiViewcontroller in your project,it means while creating any viewcontroller BaseViewController will be the parent class.

  2. Declare a variable in BaseViewController.e.g- var data: Any?

  3. Then while moving from a viewcontroller to another , simply assign any type of data to that variable declared in BaseViewController. And in any lifecycle method of your new viewcontroller you will get that data using self.data.

Y_Y
  • 331
  • 1
  • 6