Passing data from one viewcontroller to another viewcontroller

zhihaozhihao SGMember

By selecting a particular row on the TableView, it allows me to go to the next ViewController.
Questions is, how do I pass a data from that row to another ViewController ?
Below is my code for RowSelected, tableItems have 2 data, admin_num and name.
I'm trying to split the data, so that I can pass one data (admin_num) to the next ViewController.

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Assessing Student", tableItems[indexPath.Row], null, "Confirm").Show();
            tableView.DeselectRow (indexPath, true);

            string item = tableItems [indexPath.Row];
            string[] split = item.Split (new Char [] { ',' });

            parentController.PresentViewController(new AssessStudentsViewController () ,false, null);   

        }

Posts

  • MarkAnthonyHobbsMarkAnthonyHobbs GBMember
    edited April 2014

    Rather than creating your new view controller inside the PresentViewController method you could do it before hand and then pass whatever data you wanted to that object. Then just hand that object over to the PresentViewController method afterwards?

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        new UIAlertView("Assessing Student", tableItems[indexPath.Row], null, "Confirm").Show();
        tableView.DeselectRow (indexPath, true);
    
        string item = tableItems [indexPath.Row];
        string[] split = item.Split (new Char [] { ',' });
    
        AssessStudentsViewController tempViewController = new AssessStudentsViewController (); // Could use the constructor to pass in the data you want or use a method like the next line
        tempViewController.PassDataMethod(int yourData);
    
        parentController.PresentViewController(tempViewController, false, null);   
    
    }
    

    I think that would work.

    Regards
    Hobsie

  • SebastianSeidel.9226SebastianSeidel.9226 ✭✭✭✭ DEInsider, University ✭✭✭✭
    edited April 2014

    There are several ways to do it.

    1) Add an parameter to your constructor

    The AssessStudentsViewController constructor will look like this:
    public AssessStudentsViewController(string studentName).. (or whatever data type you need).

    Then call it in your RowSelected code like this:

    parentController.PresentViewController(new AssessStudentsViewController("Sebastian")...

    2) Add an property to your controller

    Your controller will be extended with a property. Inside your controller add this:
    public string StudentName { get; set; }

    In RowSelected you set it like this:

     var assessStudentViewController = new AssessStudentsViewController();
     assessStudentsViewController.StudentName = "Sebastian";
    
     parentController.PresentViewController(assessStudentsViewController, ...);
    

    3) Using a method

    This is the way @MarkAnthonyHobbs described

  • zhihaozhihao SGMember

    Thanks for the advice, @MarkAnthonyHobbs‌ and @Sebastian .
    I tried, MarkAnthonyHobbs method, it can't work on PassDataMethod.
    While it work for Sebastian method, to put a value in or a variable.
    Thank you guys!

  • JairBarzolaJairBarzola ✭✭ USMember ✭✭

    Hi guys, I have a problem I don't how to pass data to previous ViewController
    Could you help me? please

  • MondonnoMondonno ✭✭ Member ✭✭

    You'd better create a new thread @JairBarzola

Sign In or Register to comment.