How to programmatically load another viewController from a ViewController in MonoTouch

10,837

You should be able to do this if you call it on a delay. I used a Threading.Timer to call PresentViewController a second after load. As far as the delegate, a UIViewController does not have that property. You will have to cast to the controller type that is applicable to the controller you are loading. Then you can set the delegate. You will probably want to set the WeakDelegate instead of the Delegate if you want to use this (self).

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

    Timer tm = new Timer (new TimerCallback ( (state)=> {
        this.InvokeOnMainThread (new NSAction (()=> {
            UIStoryboard board = UIStoryboard.FromName ("MainStoryboard", null);
            UIViewController ctrl = (UIViewController)board.InstantiateViewController ("Number2VC");
            ctrl.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            this.PresentViewController (ctrl, true, null);
        }));
    }), null, 1000, Timeout.Infinite);
}
Share:
10,837
rams
Author by

rams

Updated on July 31, 2022

Comments

  • rams
    rams over 1 year

    I am using StoryBoards in Monotouch and have a ViewController which has been designated as the Initial VC. I have a second VC that I want to display programmatically from the ViewDidLoad method of the first VC. The Objective-C steps are like so

    1. Instantiate second VC via Storyboard.InstantiateViewController("SecondVC")
    2. Set its ModalTransitionalStyle
    3. Set its delegate to self like secondVC.delegate = self;
    4. use this.PresentViewController(secondVC, true, nil)

    How do I accomplish that in MonoTouch C# code?

    the VC that I instantiate using Storyboard.Ins.. method does not have a delegate or Delegate property to set. And although my code compiles, I do not see the second view. I see only the first View

    Any help highly appreciated

    thanks