How to programmatically load another viewController from a ViewController into MonoTouch

I use StoryBoards in Monotouch and have a ViewController that has been designated as Initial VC. I have a second VC that I want to programmatically display from the ViewDidLoad method of the first VC. Objective-C steps look like this:

  • Create a second VC via Storyboard.InstantiateViewController("SecondVC")
  • Install ModalTransitionalStyle
  • Set your delegate to yourself as secondVC.delegate = self;
  • use this.PresentViewController(secondVC, true, nil)

How to do this in MonoTouch C # code?

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

Any help is much appreciated

thanks

+7
source share
1 answer

You can do this if you call it with a delay. I used Threading.Timer to call PresentViewController a second after loading. As for the delegate, the UIViewController does not have this property. You will need to specify the type of controller that applies to the controller that you are loading. Then you can set the delegate. You might want to install WeakDelegate instead of a 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); } 
+10
source

All Articles