Numli
Numli

Reputation: 407

Navigation from within a UIApplication inheriting class

I have pondered over this for a good few hours now but to no avail. I am trying to add a method to my iPad app that sends it back to the starting screen whenever there is no activity for 120 seconds.

I am very new to both Xamarin and IOS programming so apologies if I'm approaching this from completely the wrong angle. I have created a class that looks like this

[Register("ElectronicReceptionMain")]
public class ElectronicReceptionMain : UIApplication
{       
    public override void SendEvent(UIEvent uievent)
    {
        Debug.WriteLine("SendEvent");            
        var allTouches = uievent.AllTouches;
        if(allTouches.Count > 0)
        {
            ResetIdleTimer();
        }            
        base.SendEvent(uievent);
    }

    NSTimer idleTimer;
    void ResetIdleTimer()
    {            
        idleTimer = NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(120), RefreshScreen);
    }

    void RefreshScreen(NSTimer obj)
    {
        Debug.WriteLine("Elapsed");                      
        UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null);
        ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController;
        SharedApplication.KeyWindow.RootViewController.NavigationController.PushViewController(uvc, true);

    }
}

This timer works fine, (the debug message prints out without fail). But I cannot get the NavigationController to be anything other than null obviously resulting in a null reference exception.

I also tried

uvc.NavigationController.PushViewController(uvc, true);

Same problem. I do have a navigationcontroller in my storyboard and have got successful navigation between screens when going from one UIView to another.

I have stepped through my code 1 line at a time and it is definitely the NaviagtionController that is null

Any help would be greatly appreciated.

Thanks in Advance!

Upvotes: 1

Views: 44

Answers (1)

MilanG
MilanG

Reputation: 2604

Your RootViewController should be a UINavigationViewcontroller. In Your AppDelgate you need to create an instance of the UINavigationViewController and pass your InitialViewControllerInstancein it. And store the instance of the UINavigationController in one variable(navController) as like below:

AppDelegate.cs

public UINavigationController navController;

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        //Here you need to instantiate your first ViewController instance from the storyboard and pass as an argument to the UINavigationController
        navController = new UINavigationController(yourInitialViewControllerInstance);
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        Window.RootViewController = navController;
        Window.MakeKeyAndVisible ();

        return true;
    }

Then if you want to navigate to any other View Controller from any other classes, you would simply access the instance of the navController object from AppDelgate and navigate as given below:

void RefreshScreen(NSTimer obj)
{
      Debug.WriteLine("Elapsed");                      
      UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null);
      ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController;

     //Navigate using the navController Instance from the appDelgate
     ((AppDelegate)UIApplication.SharedApplication.Delegate).navController.PushViewController(uvc, true);
}

Upvotes: 1

Related Questions