Transfer Variables in Multiscreen-App between two screens

I_WeixI_Weix Member ✭✭

Hello people,

in my Multiscreen-App an i have the following problem:

I want to transfer a variable from the second screen to the first to show it there. But the transfer isn't working...

If i read out the Variables in the first screen they are still empty.

Code from the second screen (Send):

using Foundation;
using System;
using UIKit;

namespace IPS7LnkNet
{
public partial class Einstellungen : UIViewController
{
string IPAdresse = "192.168.100.3";
string Typ = "S71500";

    public Einstellungen (IntPtr handle) : base (handle)
    { 
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        EingabeIP.Text = IPAdresse;

        var g = new UITapGestureRecognizer(() => View.EndEditing(true));
        View.AddGestureRecognizer(g);

        Test2.Text = "IP" + IPAdresse + "  " + "Typ" + Typ;
    }

    public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender) 
    {
        base.PrepareForSegue(segue, sender);

        var ViewController = segue.DestinationViewController as ViewController;

        ViewController.IP = IPAdresse;  <--- Variable 1 to send
        ViewController.SPSTyp = Typ;    <--- Variable 2 to send

    }
}

}

Code from first screen (reciever):

using System;
using UIKit;
using System.Collections.Generic;
using SPSFunktionen;

namespace IPS7LnkNet
{
public partial class ViewController : UIViewController
{
public string IP = ""; <--- The Variable1 which i want to override
public string SPSTyp = ""; <--- The Variable2 i want to override too

    public ViewController(IntPtr handle) : base(handle)
    {
    }

    partial void ButtonAuslesen_TouchUpInside(UIButton sender)
    {
        Test.Text = "IP: " + IP + "  " + "Typ: " + SPSTyp;
        try
        {
            AusgabeVariable.Text = Convert.ToString(SPSFunktionen.Funktionen.Verbindung(EingabeDatenbaustein.Text, EingabeVariable.Text, IP));
        }
        catch (IPS7Lnk.Advanced.PlcException ) 
        {
            var okAlertController = UIAlertController.Create("CPU not found!","", UIAlertControllerStyle.Alert); 
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            PresentViewController(okAlertController, true, null);

            AusgabeVariable.Text = "Connection Error";
        }

        catch (System.FormatException)
        {
            var falscheEingabe = UIAlertController.Create("Falsche Eingaben", "Bitte Eingaben überprüfen", UIAlertControllerStyle.Alert);
            falscheEingabe.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            PresentViewController(falscheEingabe, true, null);

            AusgabeVariable.Text = "Falsche Eingaben";
        }
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var g = new UITapGestureRecognizer(() => View.EndEditing(true));
        View.AddGestureRecognizer(g);
}

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

}

Thanks for Help!

Best Answers

Answers

  • I_WeixI_Weix Member ✭✭
    edited July 10

    Thank you for your Answer!
    I've implemented the code as your instructions:

    Use Events to pass data from the second page + At last trigger (...) return in the second Page Code and Register it into the Code of the Main page.

    But now i get this Error:
    Foundation.MonoTouchExceptions: Storyboard doesn't contain a view controller with identifier 'Einstellungen'
    This i have fixed with set the Storyboard ID to "Einstellungen".

    After that the error appears: System.NullReferenceException: Object reference not set to an instance of an object.

    For information: I programmed an TabBar-Application, maybe i have to use an other code?

  • I_WeixI_Weix Member ✭✭
    edited July 10

    Yes, that works, but now i get:

    System.NullReferenceException: Object reference not set to an instance of an object.
    It appears at this line: NavigationController.PushViewController(vc, true);

    i tried some things but it's not working jet..

  • LandLuLandLu Member, Xamarin Team Xamurai

    @I_Weix Have you solved it now?

  • I_WeixI_Weix Member ✭✭

    Nope, still error: System.NullReferenceException: Object reference not set to an instance of an object.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @I_Weix Which line causes that? Could you offer your sample here?

  • I_WeixI_Weix Member ✭✭

    Here:

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, please check was your current view controller in a Navigation Stack(being wrapped by a UINavigationController)?
    Secondly, make a breakpoint to check whether the vc has a valid value.

  • I_WeixI_Weix Member ✭✭

    could you show me how to?

  • LandLuLandLu Member, Xamarin Team Xamurai

    @I_Weix I didn't have your sample so I don't know what to show. Could you offer your sample here and I will look into what causes this error? Then modify it to show the correct code to you.

  • I_WeixI_Weix Member ✭✭

    Here is the Project. I had to delete the folder 'obj' because the file was to big, i hope it still works

  • LandLuLandLu Member, Xamarin Team Xamurai

    @I_Weix If you want to use NavigationController you have to wrap your view controller with a UINavigationController.
    Here is the code.

  • I_WeixI_Weix Member ✭✭
    edited July 10

    Wow thank you for your help!!

    It's working now, but only one time. If i set my values, turn back, they are saved. But if i want to change it again, the Navigation Controller isn't loading again. So i can't save them several times.

    For example: I start the app, the Controller is loading directly the second screen.. then i fill in my values and press back to main, they will be save. But if i want to change them and go to the second screen again, i cant press back again.

Sign In or Register to comment.