7

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

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
rams
  • 6,381
  • 8
  • 46
  • 65
  • Can you post some of your objective-c code to help me understand the question? – holmes May 13 '12 at 21:57
  • I am using MonoTouch. Not much Obj-C code. The steps outlined in the question are from the iOS docs on how to call a ViewController from another ViewController. I am trying to replicate the steps in C# – rams May 14 '12 at 00:08

1 Answers1

10

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);
}
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
holmes
  • 1,341
  • 9
  • 9
  • Thanks for the answer. Can you explain why a delay is needed here? – rams May 14 '12 at 00:05
  • your code worked! I am able to see the second View as expected although with a slight delay. But I don't understand why we need a timer. – rams May 14 '12 at 00:27
  • You can reduce the delay to zero milliseconds, it should still work. Also you may be able to move this code to ViewDidAppear. I found another related post [here.](http://stackoverflow.com/questions/10147625/unable-to-get-presentviewcontroller-to-work) – holmes May 14 '12 at 01:15
  • 1
    To try to answer your question more succinctly: The Timer is required because the RootController doesn't have screen bounds yet. Putting it in the timer allows the current callstack to complete and then it can complete (hence "0ms" being an OK start delay -- This is similar conceptually to doing a "setTimeout(function(){},0);" in javascript. – Andrew Theken Feb 17 '13 at 22:07
  • Instead of timer set gobal bool flag that will determine that it should launch like, shouldShowNextController = true in viewDidLoad which each controller calls only once, then in ViewDidAppear launch next ViewController if shouldShowNextController == true and set it to false – user3455363 May 05 '15 at 09:01