-1

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.

1 Answers1

0

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

edm2282
  • 156
  • 6
  • Prepare for Segue in Swift worked for me. Thanks for your help! – J3Harrelll Jul 30 '19 at 09:30
  • Part of your answer: " I'm assuming this is a Xamarin.iOS project." ; Yes it was, but I've completely rewritten the entire project using Xcode swift. From now on, everything I write for ios will be in Xcode. Thanks again for your help! – J3Harrelll Aug 16 '19 at 17:55