简体   繁体   中英

How can I use the data from one ViewController to another in order to expand the first ViewController's information?

I've written a simple Golf Scoring program using one View Controller. Everything works great. I need a second VC in order to display the results of the matches between the players (not enough room on first VC). How do I avoid getting Null values from the first VC ?

I've tried using the same class of first View Controller, but receive Delegate errors. I need the 2nd VC to inherit the values of the arrays of the 1st VC.

I'm assuming this is a Xamarin.iOS project. How are you reaching the Second VC (UINavigationController..?). A simple approach (also assuming a UINavigationController) is to create the following in the SecondVC

public int[] values;

Then in the first VC you can create the SecondVC, update the values array, and do any other logic necessary after the values array has been updated.

public OnNavigateToSecondVC() {
    var secondVC = new SecondVC();
    secondVC.values = values; // Reference to values array from first VC

    NavigationController?.PushViewController(secondVC, true);
}

Another approach is to update the constructor of the secondVC to include the ability to pass the values array directly in.

public partial class SecondVC : UIViewController
{
    int[] values;

    public SecondVC(int[] values) : base("SecondVC", null)
    {
        this.values = values;
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
    }

    public override void DidReceiveMemoryWarning()
    {
        base.DidReceiveMemoryWarning();
        // Release any cached data, images, etc that aren't in use.
    }
}

Swift also has native functionality called : PrepareForSegue

You could also use that if you want : Prepare for Segue in Swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM