Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically load another viewController from a ViewController in MonoTouch

Tags:

xamarin.ios

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

like image 787
rams Avatar asked May 13 '12 20:05

rams


People also ask

How do I move from one ViewController to another in swift 4 programmatically?

Ctrl+Drag from the “View Controller” button, to somewhere in the second View Controller(HomeViewController). It can be anywhere in the main box of the second view controller.


1 Answers

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);
}
like image 118
holmes Avatar answered Sep 23 '22 09:09

holmes