- Home /
Question by
siddharth3322 · Apr 12, 2016 at 10:47 AM ·
unity 5uibuttondelegate
Runtime Click Listener Addition
Basically I used following code to add button click listener to my list of buttons.
private void LoadCurrenWeekPicks ()
{
foreach (ReportPick reportPick in ListCollection.currentWeekPickList) {
GameObject reportPickObj = Instantiate (reportPickPref) as GameObject;
reportPickObj.transform.SetParent (reportPickContainer);
reportPickObj.transform.localScale = Vector3.one;
DayReport dayReport = reportPickObj.GetComponent<DayReport> ();
dayReport.UpdateDayNameText (reportPick.DayName);
dayReport.UpdateDateText (reportPick.Date);
dayReport.UpdateTotalTipsText (reportPick.TotalPicks);
dayReport.UpdateWinPickText (reportPick.TotalWin);
dayReport.UpdateLossPickText (reportPick.TotalLoss);
dayReport.UpdatePercentagePickText (reportPick.WinningPercentage);
Debug.Log("report date : " + reportPick.Date);
reportPickObj.GetComponent<Button> ().onClick.AddListener (delegate {
// mainMenuController.OnPaidPickButtonClick (paidPickObj);
reportController.OnReportParticularDayButtonClick(reportPick.DayName);
});
}
}
In this I want to add button click listener method call as well pass day name string argument. But in this I am facing just one problem. I was always getting last entry of day name when I print passed day name in method call.
public void OnReportParticularDayButtonClick (string selectedName)
{
// selectedDate = "12/04/2016";
// selectedDate.Replace ('-', '/');
Debug.Log("selected date : " + selectedName);
// Debug.Log ("Report : " + Convert.ToDateTime (selectedDate));
}
I am not getting as object(button) is attached with day name. I am always getting last day name entry in all click events. Please give me some hint in this.
Comment
Your answer
Follow this Question
Related Questions
UIButton highlighted color not working 0 Answers
How to keep your button selected after clicking away ??? 1 Answer
TOGGLE SCRIPT 1 Answer
how to test Buttons 1 Answer
Button rect transform top 0 Answers