我是iOS开发的初学者,我使用UICollectionView实现了以下屏幕:
CollectionViewCell文件代码是:
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)
}
}
}
现在单击Leaderboard按钮(带1,2,3的图标)我想打开新的LeaderBoard Controller并传递matchId,它是classEvent.id
如何将值传递给新控制器?什么是最好的方法。
答案 0 :(得分:2)
您可以通过segue传递匹配ID:
在LeaderBoard Controller中设置属性:
var matchId:Int?
在控制器之间设置segue并添加标识符: 单击排行榜按钮:
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)
添加segue方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue,identifier == "yourSegueIdentifier" {
let destinationVC = segue.destinationViewController as LeaderbardController
destinationVC.matchId = classEvent.id
}
}
}
答案 1 :(得分:-1)
获得你想要的三个简单步骤:
使BaseViewController类成为UiViewController的子类。这个类将是项目中UiViewcontroller的替代,它意味着在创建任何viewcontroller时BaseViewController将是父类。
在BaseViewController.e.g中声明一个变量 - var data:Any?
然后在从viewcontroller移动到另一个时,只需将任何类型的数据分配给BaseViewController中声明的变量。 在新的viewcontroller的任何生命周期方法中,您将使用self.data获取该数据。