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...
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!
When we return back from the second view controller PrepareForSegue
won't be triggered. Its function is to notify the view controller that a segue is about to be performed: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepareforsegue.
Use events to pass data from the second page:
// Deine these in your second view controller public delegate void PassParameters(string ip, string type); public event PassParameters PassParametersEvent;
Register it when you pushed to the second page:
Einstellungen vc = Storyboard.InstantiateViewController("Einstellungen") as Einstellungen; vc.PassParametersEvent += (ip, type) => { this.IP = ip; this.SPSTyp = type; }; NavigationController.PushViewController(vc, true);
At last trigger this event when you click back button to return:
public override void ViewWillDisappear(bool animated) { base.ViewWillDisappear(animated); if (IsMovingFromParentViewController) { PassParametersEvent?.Invoke(IPAdresse, Typ); } }
@I_Weix Change this line Einstellungen vc = Storyboard.InstantiateViewController("Einstellungen") as Einstellungen;
to your own. I consumed it to create my view controller. If you are using storyboard too you have to set the storyboard ID here:
Otherwise, the error will throw out as it can't find the corresponding view controller in the storyboard.
If you didn't use a storyboard. Just change it to new Einstellungen(...)
.
Answers
When we return back from the second view controller
PrepareForSegue
won't be triggered. Its function is to notify the view controller that a segue is about to be performed: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepareforsegue.Use events to pass data from the second page:
Register it when you pushed to the second page:
At last trigger this event when you click back button to return:
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_Weix Change this line
Einstellungen vc = Storyboard.InstantiateViewController("Einstellungen") as Einstellungen;
to your own. I consumed it to create my view controller. If you are using storyboard too you have to set the storyboard ID here:Otherwise, the error will throw out as it can't find the corresponding view controller in the storyboard.
If you didn't use a storyboard. Just change it to
new Einstellungen(...)
.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..
@I_Weix Have you solved it now?
Nope, still error: System.NullReferenceException: Object reference not set to an instance of an object.
@I_Weix Which line causes that? Could you offer your sample here?
Here:
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.could you show me how to?
@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.
Here is the Project. I had to delete the folder 'obj' because the file was to big, i hope it still works
@I_Weix If you want to use
NavigationController
you have to wrap your view controller with a UINavigationController.Here is the code.
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.